小编Rch*_*han的帖子

使用不同的http请求时"保存"和"更新"之间的区别

当我在下面的代码中尝试替换@post.update@post.save,它仍然有效并返回true,但值未更新.

 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
Run Code Online (Sandbox Code Playgroud)

以下是我的佣金路线:

$ rake routes
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

ruby-on-rails ×1