Hi, I’m in page 94 following along, and running the first curl
throws the error mentioned in the title.
This is my wikicat project in case you wanted to check.
Rails 4.2.4
Ruby 2.2.2p95
Hey,
did you defined new routes without restarting the server?
I wouldn’t open an issue without having put a fair amount of fight )) But yeah, I restarted the router like probably a dozen times, and went over the code (it wasn’t much, really) quite a few times.
Could you use the following syntax in your routes?
get '/category/:category', :to => 'category#show'
Also please respect the spaces. I know it doesn’t matter but it makes reading the code easier and everything more consistent for larger projects.
Then you will have the respond_to error. This is because of the updates to Rails and Rails-api. If you downgrade the gems version as in my original repo you will be fine.
Thanks for reaching out!!
Silvia
Thanks Silvia, but that syntax is allowed in hashes with symbol keys since version 1.9.. Also, in your original repo, none of the gems in your Gemfile
(except sdoc
) include a version, only Rails does.
One of the things I’ve noticed in your repo is that your ApplicationController
inherits from ActionController::Base
whereas mine inherits from ActionController::API
. Also, you have a views/layout
folder with application.html.erb
layout in it. It looks like you have generated your app with rails new
instead of rails-api new
.
Hey there!
Yes the syntax is allowed, but something in the way you write it gets it messed. I cloned your repository and did the change suggested and it works. I will send you a PR so you can try it out.
Also there are a few little differences between rails and rails-api, but these shouldn’t impact at all the way you build the API at this stage.
To check the version I used in my repository check the Gemfile.lock file.
So you say my project works on your machine with the edit on routes? I still get the same error. What’s your ruby version?
$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
Oh boy, I deleted the project folder, cloned my repo, bundle update(This time I change Rails in the Gemfile to the version you use), and now am getting NoMethodError undefined method
raise_in_transactional_callbacks=’ for #Class:0x007fba2b575f98`
Does that make any sense?
I sent you a PR regarding the routes. Also please do not run bundle update, use the version from my repo by looking at the Gemfile.lock.
Yeah, I merged your PR. Now, I’ve copy/paste your Gemfile.lock, run bundle, and same weird NoMethodError undefined methodraise_in_transactional_callbacks
.
Do you think does have anything to do with dropping the db? Before, when I started with your book, I tried the dumps, before I realize one of them was > 10 gb 😔 , and had cut it out in the middle when I was running out of space in my HDD and DROP the db.
Then I took the seed file solution, and seeded it, of course.
It is better to use the information in ghe Gemfile.lock to specify a specific version of a gem in your Gemfile.
If you have dropped the db, you have to run rake db:setup.
Also I have sent a second PR, please check it.
Wow, girl you are tough, I don’t know where are you based but here in Moscow(I’m spanish though) is past 5, so I’m gonna wrap up. Thanks for your help.
Hey Silvia, I got it working. It was indeed a silly eye-straining error on my side, that slipped our defenses:
get 'category/:category', :to => 'category/#show'
See that slash before the #show
action? 🙀
Anyways, I ended up starting over here wicked where I used the most up to date versions of all gems, hopefully I won’t find problems along the line.
Indeed I hit the respond_to
error you mentioned, because from Rails 4.2.*, class-level methods such as respond_to
have been moved to the responders gem, so instead of reverting to an older Rails version, I just added it to my Gemfile
.
Hey @bifork,
Thanks for your comments, some of the errors are related to old version of Rails. I am sorry about those, but I wrote the book 2 years ago now. I am planning on releasing an update, hopefully in the spring.
Thank @hiromipaw for your reply, Silvia.
Though I cannot hope for a big revamp in your spring release. But I do wish you could take time to expand your wonderful topics to greater detail. Right now, your book seems to be friendly to really experienced developers.
I wish you could include the user model and authentication solution from the start. Then a RESTful resources owned by a user and conversations/comments about a RESTful resource object between users. Besides the relationship/association between models, if you can include a transaction like payment or ownership transfer, it would be all too great.
I can imagine you are incredibly busy, especially for a girl who pursues highest academic degree besides cutting-edge programming skills. But I believe, if you spend time to write an awesome popular book, you can make money while you’re sleeping or playing on your beautiful beaches.
I wish I could be a great author after becoming very skilled. But I’m not experienced yet and still slowly researching. A foggy world to me right now.
Muchas gracias:)
Adding this answer to get clarity on namespace & scope.
When you use namespace, it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.
# config/routes.rb
namespace :admin do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
When we add scope, it will just map the controller action for the given scope patterns. No need to define controller under any module.
# config/routes.rb
scope :admin do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) posts#index
Note that, controller is just posts controller without any module namespace.
If we add a path
option to scope it will map to the controller with the path option specified as follows
# config/routes.rb
scope module: 'admin', path: 'admin' do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
Note that the controller now is under admin module.
Now, if we want to change the name of path method to identify resource, we can add as
option to scope.
# config/routes.rb
namespace module: 'admin', path: 'admin', as: 'root' do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
root_posts GET /admin/posts(.:format) admin/posts#index
You can see the change in the Prefix Verb.
Hope it helps others.
According to http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing you should use scope instead of namespace.
If you want to route /admin/posts
to PostsController
(without the Admin:: module prefix
), you could use:
scope "/admin" do
resources :posts, :comments
end
Late answer, but still might be helpful:
scope '/v1' do
resources :articles, module: 'v1'
end
controller
# app/controller/v1/articles_controller.rb
class V1::ArticlesController < ApplicationController
end
Now you should be able to access this url:
http://localhost:3000/v1/articles
I’m trying to set up routes for a mobile API, which should have a versioned api-path. I already could make the mobile Auth work, which is implemented in a separate Controller AuthController located in /controllers/api/v1/mobile/.
Usage example:
myapp.com/api/v1/mobile/auth
But now I want to register my existing ressources-Controllers to this path-pattern as additional api-routes. Concrete: this would be the TasksController located at /controllers/tracker/tasks_controller.rb. So I added a mobile route to the routes-definition:
# routes.rb
namespace :tracker, path: 'timetracking' do
resources :tasks, 'jobs'
end
namespace :api do
namespace :v1 do
namespace :mobile do
resources :auth, :only => [:create, :destroy]
namespace :tracker do #added mobile route
resource :tasks, controller: 'tracker/tasks', as: :mobile_tasks
end
end
end
end
But when I call myapp.com/api/v1/mobile/tracker/tasks it results in an error-message:
Routing Error
uninitialized constant Api::V1::Mobile::Tracker
I especially added the alias :mobile_tasks to this route, to avoid any conflicts with the original tasks-route above. Any ideas, how to set the controller properly for this route?
Update#1
Defining this route as a scope instead of a namespace, didn’t work aswell.
scope "/api/v1/mobile/tracker" do
resources :tasks, controller: 'tracker/tasks', as: :mobile_tasks
end
But this time, it didn’t even resolve the route-path itself.
Routing Error
No route matches [GET] "/api/v1/mobile/tracker/tasks"
I assume it might be a problem, that my additional mobile-api route tries to point to a completely different namespace tracker.
According to http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing you should use scope instead of namespace.
If you want to route /admin/posts
to PostsController
(without the Admin:: module prefix
), you could use:
scope "/admin" do
resources :posts, :comments
end
Adding this answer to get clarity on namespace & scope.
When you use namespace, it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.
# config/routes.rb
namespace :admin do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
When we add scope, it will just map the controller action for the given scope patterns. No need to define controller under any module.
# config/routes.rb
scope :admin do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) posts#index
Note that, controller is just posts controller without any module namespace.
If we add a path
option to scope it will map to the controller with the path option specified as follows
# config/routes.rb
scope module: 'admin', path: 'admin' do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
Note that the controller now is under admin module.
Now, if we want to change the name of path method to identify resource, we can add as
option to scope.
# config/routes.rb
namespace module: 'admin', path: 'admin', as: 'root' do
resources :posts, only: [:index]
end
# rake routes
Prefix Verb URI Pattern Controller#Action
root_posts GET /admin/posts(.:format) admin/posts#index
You can see the change in the Prefix Verb.
Hope it helps others.
Late answer, but still might be helpful:
scope '/v1' do
resources :articles, module: 'v1'
end
controller
# app/controller/v1/articles_controller.rb
class V1::ArticlesController < ApplicationController
end
Now you should be able to access this url:
http://localhost:3000/v1/articles