when I do
rails g model user name:string
rails g controller users index create new destroy show
and edit config/routes.rb to add:
resource :users
bundle exec rake routes gives:
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_users GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_users GET /users/edit(.:format) {:action=>"edit", :controller=>"users"}
GET /users(.:format) {:action=>"show", :controller=>"users"}
PUT /users(.:format) {:action=>"update", :controller=>"users"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"users"}
however, when I do
rails g resource users name:string
(which automatically adds resources :users to config/routes.rb)
bundle exec rake routes
I get
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
So my question is,
when I generate a controller how can I get the correct helper methods to make
link_to ‘Destroy’, instance, :method=> :delete
work?
Because currently it gives an error user_path is not defined.
You should call
instead of
in order to get
resources :usersto give you the helpers you want.The latter causes Rails to assume that
usersis a singular object, and thatresources :usersshould create what is called a singular resource:http://guides.rubyonrails.org/routing.html#singular-resources
as a result,
user_pathis undefined, whereasusers_pathis defined.