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

Viewed 26441

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.

3 Answers
Related