开发者

In Rails, is the RESTful standard that a GET to /users is for index action, and a POST to /users is for create action?

开发者 https://www.devze.com 2023-02-13 12:29 出处:网络
I am looking into a Rails plugin, and it seems that to create a user, the HTML form says <form action=\"/users\" method=\"post\">

I am looking into a Rails plugin, and it seems that to create a user, the HTML form says

<form action="/users" method="post">

and if I do a

rake routes

it says:

users GET    /users(.:format)           {:controller=>"users", :action=>"index"}
      POST   /users(.:format)           {:controller=>"users", :action=>"create"}

so looks like a standard is that a GET to /controller_name is to perform the index action, while a POST is to per开发者_如何转开发form the create action? Is this almost the 100% standard? Is there any exception?


Ther answer is NO

Rails routs are as flexible as you can imagine.

BUT. Rails loves REST style. You can read some wiki http://en.wikipedia.org/wiki/Representational_State_Transfer

REST like CRUD: http://en.wikipedia.org/wiki/Create,_read,_update_and_delete

So. We have got convention about resources. We can:

  • READ list of resources: GET /resources
  • READ resource: GET /resources/:id
  • CREATE new resource: POST /resources
  • UPDATE resource: PUT /resources/:id
  • DELETE resource: DELETE /resources/:id
  • READ resource for edit: GET /resources/:id/edit
  • READ for creating: GET /resources/new

This is a basis of REST.


This is the default when using Rails' resourceful routes. You can of course override this default in many ways, as described in this guide, but you should only do so with good reason.

0

精彩评论

暂无评论...
验证码 换一张
取 消