Rails new vs create

前端 未结 4 504
终归单人心
终归单人心 2020-11-28 17:56

Why is there a need to define a new method in RESTful controller, follow it up with a create method?

Google search didn\'t provide me the answer I was looking for. I

4条回答
  •  无人及你
    2020-11-28 18:23

    The RESTful parts of Rails are made to be very close to how the HTTP protocol works. In the HTTP protocol, a GET request isn't supposed to modify any data. Logically, if you look at the way all of the RESTful actions in Rails work, they will match up with HTTP actions. A POST is for generating new data, so it is logically create. You use a GET to serve the form version of that or in other words, the new action. Index and show are also GETs, update is a PUT (or PATCH in Rails 4+), and destroy is a DELETE in HTTP.

    In addition, it nicely separates the logic in the controller and gives you a smooth way to deal with errors (by re-rendering the new action with error messages).

提交回复
热议问题