加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何重定向到页面中的某个位置?

发布时间:2020-12-17 03:36:32 所属栏目:百科 来源:网络整理
导读:我有一个带有评论的博客应用程序. 目前,评论控制器具有标准的创建操作 def create @comment = current_user.build(params[:comment]) respond_to do |format| if @comment.save format.html { redirect_to @comment.post } endend 创建后,用户将被重定向到发
我有一个带有评论的博客应用程序.
目前,评论控制器具有标准的创建操作

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,然后在我的控制器中引用?我需要以某种方式“保存”位置吗?感谢您帮助新手!

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读