When I tried replacing @post.update
with @post.save
as in my code below, it still worked and it returned true, but the values were not updated.
def create
@post = Post.new(post_params)
if @post.save
redirect_to posts_path, notice: 'Post was successfully created.'
else
render action: 'new'
end
end
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
below are my rake routes:
$ rake routes
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root / welcome#index
Why didn't it update or overwrite my record?
Will using different http requests for the same methods have any effect on them? Can we use PUT
, GET
, PATCH
and DELETE
for save when passed with proper syntax?
The question is regarding rails 4 guide, the first guide.
The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to the CRUD operations (create, read, update, and delete). GET: GET request is used to read/retrieve data from a web server.
If the Request-URI refers to an already existing resource, an update operation will happen, otherwise create operation should happen if Request-URI is a valid resource URI (assuming client is allowed to determine resource identifier). request as a new subordinate of the resource identified by the Request-URI in the Request-Line.
POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form. Example –
Not everyone is as intuitive as you might think. Update and Save are largely interchangeable. The only difference is, Save typically implies a page-change event, whereas Update usually implies that changes will be applied to what you can see, immediately (technologically speaking, usually accompanies an AJAX operation.)
Because save
will not accept the attributes as parameters; save
can only accept parameters like validate: false
to skip validation.
If you want to use save
, then you need to assign or modify individual attributes before save
. But if you want mass-assignment, update
would be your choice.
@post.f_name = 'foo'
@post.l_name = 'bar'
@post.update # This will not work
@post.save # This will work
@post.save({:f_name=>"peter",:l_name=>"parker"}) # This will not work
@post.update({:f_name=>"peter",:l_name=>"parker"}) # This will work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With