Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Put vs Post

I have been reading up on the difference between put and post requests and I have some related questions as it pertains to rails: I would like to change one specific field in an already created row...should I use a put or a post request? For example are the following different?

#Assume this is a put request
def update
    @model=Model.find(x)
    @model.field="new_field"
    @model.save
end

#Assume this is a post request
def update
    @model=Model.find(x)
    @model.field="new_field"
    @model.save
end

#What if I use the rails update method?
def update
    @model=Model.find(x)
    @model.update(model_params)
    @model.save
end

Thanks in advance.

like image 945
kempchee Avatar asked Oct 21 '13 20:10

kempchee


People also ask

What is difference between put and POST?

The difference between POST and PUT is that PUT requests are idempotent. That is, calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly have side effects of creating the same resource multiple times.

Which is more secure POST or put?

POST is more secure than GET for a couple of reasons. GET parameters are passed via URL. This means that parameters are stored in server logs, and browser history. When using GET, it makes it very easy to alter the data being submitted the the server as well, as it is right there in the address bar to play with.

What is difference between PUT and POST IN REST API?

Another important difference between the methods is that PUT is an idempotent method, while POST isn't. For instance, calling the PUT method multiple times will either create or update the same resource. In contrast, multiple POST requests will lead to the creation of the same resource multiple times.

What is the difference between put patch and POST?

Here is a simple description of all: POST is always for creating a resource ( does not matter if it was duplicated ) PUT is for checking if resource exists then update, else create new resource. PATCH is always for updating a resource.


1 Answers

According to rails convention,

PUT is used for updating an existing resource

POST is used for creating a new resource

In rails 4, PUT has been changed to PATCH to avoid confusion.

Rails generated routes will look like below by default

    posts GET    /posts(.:format)                            {:action=>"index", :controller=>"posts"}
          POST   /posts(.:format)                            {:action=>"create", :controller=>"posts"}
 new_post GET    /posts/new(.:format)                        {:action=>"new", :controller=>"posts"}
edit_post GET    /posts/:id/edit(.:format)                   {:action=>"edit", :controller=>"posts"}
     post GET    /posts/:id(.:format)                        {:action=>"show", :controller=>"posts"}
          PUT    /posts/:id(.:format)                        {:action=>"update", :controller=>"posts"}
          DELETE /posts/:id(.:format)                        {:action=>"destroy", :controller=>"posts"}

Notice the action for PUT and POST

like image 57
usha Avatar answered Sep 18 '22 22:09

usha