Rails POST, PUT, GET

After creating the scaffold, Rails gives me the opportunity to POST on items.xml , which will create a new item . GET to items.xml will just list them all. Where does Rails determine which method in the controller ( create or index , respectively) will be called, depending on the type of action I perform?

In particular, POST calls method A, but GET for the same method of calling URL.B. Where is this indicated? Where does Rails make a definition to call the controller's index method?

+52
ruby ruby-on-rails web-services
Apr 22 '09 at 17:43
source share
8 answers

I believe it is specified by REST . Here is the 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 of these routes, in config / routes.rb just add map.resources :items

+86
Apr 22 '09 at 17:47
source share

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

 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 since web browsers usually only support GET and POST, Rails uses a hidden field to turn them into PUT and DELETE requests, if necessary.

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

+27
Apr 22 '09 at 19:51
source share

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

+10
Apr 22 '09 at 17:48
source share

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

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

.. etc. This is for Rails 3, by the way, and will create a single resource for creating POST. Or, if you only need a small set from the REST set, simply:

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

etc.

+6
Apr 26 2018-12-12T00:
source share

As Don Werver said, take a look at the routes.rb file. There you have something like this:

 map.resources :items 

Here, the rails associate POST and GET requests with specific actions. To see how this works, see the links from other answers. Documents help a ton.

In all the routes and activities that they associate with you, you can enter rake routes at the command line when you are in the root of your rails directory. This will show you everything (in terms of routing) that the scaffold gives you.

+5
Apr 22 '09 at 17:54
source share

This will help a lot, but it is not a direct answer to your question. The following command will display the mappings used by your application, so you don’t need to remember all the details or guess.

 $ rake routes 

To answer more directly, this is an agreement that uses rails. You set this display when you put something like the following in your routes. Rb

 map.resources :items 
+5
Apr 22 '09 at 19:14
source share

map.resources is a method that automatically provides you with REST routes and path helpers. This is a nice feature if you already know and understand how quiet rail routing works, but it is also an obstacle to exploring rails because too much is hidden.

Railsguides has a pleasant route .

+3
Apr 22 '09 at 20:00
source share

To be honest, you can't go wrong with the routing documentation on the Rails website. This helped to take the next steps and go beyond the resources of the resources (which is okay for most applications) and really nail down reliable routing functions.

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

+1
Apr 25 '13 at 14:57
source share



All Articles