Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'save' and 'update' in using with different http requests

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.

like image 320
Rchauhan Avatar asked Dec 26 '13 05:12

Rchauhan


People also ask

What are the different types of HTTP request?

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.

What is the difference between update and create request in request-URI?

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.

What is POST request in http?

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 –

What is the difference between update and save in Salesforce?

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.)


1 Answers

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
like image 58
Siva Avatar answered Oct 09 '22 02:10

Siva