Rails POST, PUT, GET

前端 未结 8 1336
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 14:14

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 sim

相关标签:
8条回答
  • 2020-12-12 14:49

    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

    0 讨论(0)
  • 2020-12-12 14:51

    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

    0 讨论(0)
  • 2020-12-12 14:56

    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.

    • See Rails Routing from the Outside In for more detail.
    0 讨论(0)
  • 2020-12-12 14:56

    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.

    0 讨论(0)
  • 2020-12-12 15:06

    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.

    0 讨论(0)
  • 2020-12-12 15:06

    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
    
    0 讨论(0)
提交回复
热议问题