58

After I generate a scaffold, Rails gives me the ability to POST to items.xml which will create a new item. A GET to items.xml will simply list them all. Where does Rails specify which method in the controller (create or index, respectively) will be called, based on the type of action I am performing?

More specifically, POST calls methodA but GET to the same URL calls methodB. Where is this specified? Where does Rails make the determination to call the index method of the controller?

John Topley
  • 113,588
  • 46
  • 195
  • 237
geowa4
  • 40,390
  • 17
  • 88
  • 107

8 Answers8

95

I believe it's specified by REST. Here's a list for ya:

GET    /items        #=> index
GET    /items/1      #=> show
GET    /items/new    #=> new
GET    /items/1/edit #=> edit
PUT    /items/1      #=> update
POST   /items        #=> create
DELETE /items/1      #=> destroy

Edited to add to get all those routes, in config/routes.rb, simply add map.resources :items

Matt Grande
  • 11,964
  • 6
  • 62
  • 89
  • 40
    Just type "rake routes" to see what's up. – Tom Lehman Apr 22 '09 at 18:58
  • And is there a way to explicitly specify that I want to use POST only like in ASP.NET MVC ([HttPost] attribute)? – Alex Oct 17 '10 at 16:11
  • If you perform a GET to /items, you will go to the Index action. If you POST to /items, you will go to Create. – Matt Grande Oct 19 '10 at 15:22
  • 1
    @Alex: you can specify to only use POST by adding :via => :POST to the end of the route. Likewise if you only want POST and GET (and not PUT, DELETE, etc.) you'd use :via => [:POST, :GET] – mdpatrick Jul 08 '12 at 22:47
  • 3
    It's not "specified by REST", it's merely a convention in Rails. The REST specification has nothing to do with how a framework should handle a request, including which method gets called. – Asfand Qazi Sep 03 '14 at 12:47
  • I'd say it's at least a *part* of REST. See the wiki article I liked to: https://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services – Matt Grande Sep 04 '14 at 15:42
27

Rails defines seven controller methods for RESTful resources by convention. They are:

Action   HTTP Method  Purpose
-------------------------------------------------------------------------
index    GET          Displays a collection of resources
show     GET          Displays a single resource
new      GET          Displays a form for creating a new resource
create   POST         Creates a new resource (new submits to this)
edit     GET          Displays a form for editing an existing resource
update   PUT          Updates an existing resource (edit submits to this)
destroy  DELETE       Destroys a single resource

Note that because web browsers generally only support GET and POST, Rails uses a hidden field to turn these into PUT and DELETE requests as appropriate.

Specifying map.resources :items in config/routes.rb gets you those seven methods "for free". You can list all the routes within your application at any time by entering rake routes in the console.

John Topley
  • 113,588
  • 46
  • 195
  • 237
  • Also you can check out the source code to "reproduce" any of those behaviors: [Rails v3.2.9 resources](https://github.com/rails/rails/blob/v3.2.9/actionpack/lib/action_dispatch/routing/mapper.rb#L984) at actionpack/lib/action_dispatch/routing/mapper.rb – brutuscat Nov 15 '12 at 10:20
10

The best place to learn about this would be the Routing Guide.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
Don Werve
  • 5,100
  • 2
  • 26
  • 32
6

Did you want to know how to use POST only? Do this, for example:

resources :items, :only => [:create]

..etc. This is for Rails 3 by the way, and will generate a single resource to POST create. Or if you only need a really small subset of the REST set, just:

match 'items/:id' => "items#create', :via => :post

etc etc.

JJD
  • 50,076
  • 60
  • 203
  • 339
buddhamagnet
  • 230
  • 2
  • 11
5

Like Don Werve said, take a look at your routes.rb file. In there you probably have something like this:

map.resources :items

This is where rails links the POST and GET requests to certain actions. To see how this works look at the links from the other answers. The docs help a ton.

To all the routes and which actions they link to you can type rake routes into the command prompt when you are in the root of your rails directory. This will show you everything (in terms of routing) that a scaffold gives you.

vrish88
  • 20,047
  • 8
  • 38
  • 56
5

This will help a lot, but it's not a direct answer to your question. The following command will list the mappings your app uses so you don't have to remember all the details or guess.

$ rake routes

To answer more directly, this is a convention that rails uses. You set this mapping up when you put something like the following in your routes.rb

map.resources :items
jshen
  • 11,507
  • 7
  • 37
  • 59
3

map.resources is a method that automagically gives you the REST routes and path helpers as well. This is a nice feature if you already know and understand how rails' restful routing works but it is also a bit of a hindrance for learning rails because too much is hidden.

Railsguides has a nice routes guide.

srboisvert
  • 12,679
  • 15
  • 63
  • 87
1

To be honest, you can't really go wrong with the routing documentation on the Rails website. This has helped take the next steps and move beyond the comfort of resources (which for most apps is fine)and really nail down the solid routing features available.

http://guides.rubyonrails.org/routing.html

Michael
  • 1,786
  • 5
  • 23
  • 42