簡體   English   中英

如何重定向到頁面中的某個位置?

[英]How to redirect to a certain location in a page?

我有一個帶有評論的博客應用程序。 目前,評論控制器具有標准的創建操作

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

創建后,用戶將被重定向到發表評論的博客文章。 如何在頁面中向下重定向到新評論的位置?

文章/ show.html.erb

<div id="post_show">
  <%= @post.content %>
  <%= render @post.comments %>
</div>

評論/ _comment.html.erb

<div id="comment_partial">
  <%= comment.content %>
</div>

有什么東西我可以添加到我的HTML,然后在我的控制器中引用? 我需要以某種方式“保存”位置嗎? 感謝您幫助新手!

您可以在路徑助手中使用anchor選項,例如

redirect_to post_path(@comment.post, anchor: 'some-id')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM