Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between resource and controller generators

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.

like image 205
nurettin Avatar asked Jun 02 '11 15:06

nurettin


People also ask

What is a resource in Rails?

Any object that you want users to be able to access via URI and perform CRUD (or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generally a database table which is represented by a model, and acted on through a controller.

What is controllers in Ruby?

A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class.

What is a controller action Rails?

In the Rails architecture, Action Controller receives incoming requests and hands off each request to a particular action. Action Controller is tightly integrated with Action View; together they form Action Pack. Action Controllers, or just “controllers,” are classes that inherit from ActionController::Base .

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.


1 Answers

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
like image 117
artamonovdev Avatar answered Nov 02 '22 19:11

artamonovdev