Ruby on rails 无法在Ruby on Rails中更新帖子

Ruby on rails 无法在Ruby on Rails中更新帖子,ruby-on-rails,Ruby On Rails,我正在制作一个类似博客的应用程序,并在编辑时更新帖子 我使用一个名为_post_表单的部分表单来编辑帖子: <%= form_for(@post) do |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="field"> <%= f.text_area :content, placeholder: "Compose new post..." %> &l

我正在制作一个类似博客的应用程序,并在编辑时更新帖子

我使用一个名为_post_表单的部分表单来编辑帖子:

<%= form_for(@post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new post..." %>
</div>
<div id="post_button">
<%= f.submit "Post", class: "btn btn-primary" %>
</div>
<% end %>

来自my posts controller的相关代码:

class PostsController < ApplicationController
before_action :find_note, only: [:show, :edit, :update]

def update
  redirect_to @post
end

def find_note
  @post = Post.find(params[:id])
end
class PostsController
当我点击“Post”按钮时,它会将我重定向到正确的Post,但实际上它不会用我在表单中输入的新文本更新它。我觉得我缺少一些基本的东西,但我不确定是什么


感谢您的帮助

您缺少model
update
调用
PostsController\update
操作,这就是您的post记录未得到更新的原因。在
PostsController#Update
操作中重定向之前更新post记录

def update
  @post.update(post_params) ## <- add this
  redirect_to @post
end
def更新

@post.update(post_参数)##=4,是
post_参数

中的白名单属性(强参数),您缺少模型
update
调用
PostsController#update
操作,这就是您的post记录没有得到更新的原因。在
PostsController#Update
操作中重定向之前更新post记录

def update
  @post.update(post_params) ## <- add this
  redirect_to @post
end
def更新

@update(post_参数)##=4,是
post_参数中的白名单属性(强参数)
您没有更新控制器中的任何内容,只是将用户重定向到
post
视图

首先获取
post
的新值:

  def post_params
    params.require(:post).permit(:content)
  end
然后在重定向之前对其进行更新:

def update
  @post.update(post_params)
  redirect_to @post
end
总而言之,您的控制器应该如下所示:

class PostsController < ApplicationController
  before_action :find_note, only: [:show, :edit, :update]

  def update
    @post.update(post_params)
    redirect_to @post
  end

  private

  def post_params
    params.require(:post).permit(:content)
  end

  def find_note
    @post = Post.find(params[:id])
  end
end
class PostsController
您没有更新控制器中的任何内容,您只是将用户重定向到
post
视图

首先获取
post
的新值:

  def post_params
    params.require(:post).permit(:content)
  end
然后在重定向之前对其进行更新:

def update
  @post.update(post_params)
  redirect_to @post
end
总而言之,您的控制器应该如下所示:

class PostsController < ApplicationController
  before_action :find_note, only: [:show, :edit, :update]

  def update
    @post.update(post_params)
    redirect_to @post
  end

  private

  def post_params
    params.require(:post).permit(:content)
  end

  def find_note
    @post = Post.find(params[:id])
  end
end
class PostsController
谢谢你的回答,我现在明白多了!谢谢你的回答,我现在明白多了!