Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get, match and resources in routes.rb

I am new to Rails. I found it very strange when I use the resources in routes.rb, after I redirect the page to controller/index, it render the controller/show .

I know GET controller/action is same as match "controller/action", :to => "controller/action"

I think the strange thing happens to me about the redirect, is similar to the GET and Match.

so I wonder what exactly the resources mean, can I use some simple match do the same thing?

like image 878
mko Avatar asked Dec 12 '10 07:12

mko


1 Answers

resources is a shortcut for generating seven routes needed for a REST interface.

resources :widgets is equivalent to writing

get    "widgets"          => "widgets#index",   :as => 'widgets'
get    "widgets/:id"      => "widgets#show",    :as => 'widget'
get    "widgets/new"      => "widgets#new",     :as => 'new_widget'
post   "widgets"          => "widgets#create",  :as => 'widgets'
get    "widgets/:id/edit" => "widgets#edit",    :as => 'edit_widget'
patch  "widgets/:id"      => "widgets#update",  :as => 'widget'
put    "widgets/:id"      => "widgets#update",  :as => 'widget'
delete "widgets/:id"      => "widgets#destroy", :as => 'widget'

it just saves you the trouble.

By the way, get is not exactly the same as match. get, post, put and delete are shortcuts for limiting the route to a single HTTP verb. The two route definitions below are equivalent.

match 'foo' => 'controller#action', :method => :get
get   'foo' => 'controller#action'
like image 109
edgerunner Avatar answered Oct 28 '22 13:10

edgerunner