如何重定向到页面中的某个位置?

ume*_*ezo 5 redirect ruby-on-rails

我有一个带有评论的博客应用程序.目前,评论控制器具有标准的创建操作

def create    
  @comment = current_user.build(params[:comment])
  respond_to do |format|
  if @comment.save
    format.html { redirect_to @comment.post }
  end
end
Run Code Online (Sandbox Code Playgroud)

创建后,用户将被重定向到发表评论的博客文章.如何在页面中向下重定向到新评论的位置?

文章/ show.html.erb

<div id="post_show">
  <%= @post.content %>
  <%= render @post.comments %>
</div>
Run Code Online (Sandbox Code Playgroud)

评论/ _comment.html.erb

<div id="comment_partial">
  <%= comment.content %>
</div>
Run Code Online (Sandbox Code Playgroud)

有什么东西我可以添加到我的HTML,然后在我的控制器中引用?我需要以某种方式"保存"位置吗?感谢您帮助新手!

Ahm*_*rif 8

您可以anchor在路径助手中使用该选项,例如

redirect_to post_path(@comment.post, anchor: 'some-id')
Run Code Online (Sandbox Code Playgroud)