12
votes

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.

4
Can you post the contents of your routes.rb file in both cases for the user resource? Also, please separate commands with line breaks for clarity.Matthew Lehner

4 Answers

13
votes

You should call

rails g controller user index create new destroy show

instead of

rails g controller users index create new destroy show

in order to get resources :users to give you the helpers you want.

The latter causes Rails to assume that users is a singular object, and that resources :users should create what is called a singular resource:

http://guides.rubyonrails.org/routing.html#singular-resources

as a result, user_path is undefined, whereas users_path is defined.

1
votes

When you use rails g controller and specify the method names, the generator only maps specific routes to the routes file. rails g resource assumes that you want the whole resource functionality and will map resources.

In order to fix this, just go into your routes file and replace the specific mappings with a resources call.

resources :users
0
votes

What I really wanted was a way of creating a working (with correct delete/show paths) controller for an existing model (as described in the question) but just adding "resource :x" and generating the controller wasn't enough.

I ended up using the scaffold_controller generator. It doesn't create any migrations or models, but it does generate a resource with views and rake paths command shows the correct paths for delete and show to work.

0
votes

You can run following commands in the console:

$rails g model user name:string
$rails g scaffold_controller User

And add this code line to the file routes.rb:

resources :users