【发布时间】:2012-11-27 10:34:21
【问题描述】:
我有一个博客应用程序,其中包含可以通过帖子控制器中的vote 操作接收投票的帖子。
因为我允许在 index 和 show 视图中投票,所以我在投票后redirect_to :back,如下所示。
def vote
# voting logic goes here
redirect_to :back
end
这会将我重定向到正确的页面,但我想重定向到页面中的特定帖子。为此,我在帖子部分 div 中添加了一个识别锚。
<div id="post_<%= post.id %>">
<%= post.content %>
</div>
如何在我的redirect_to :back 中引用它?我尝试了以下方法,但不起作用。
# this doesn't work!
redirect_to :back, anchor: "post_#{@post.id}"
或者,如果我要为显示和索引视图使用 if 子句,我该怎么做?我尝试了以下,返回undefined method 'current_page' for VocabsController。
#this doesn't work!
if current_page?(posts_path)
redirect_to posts_path(anchor: "post_#{@post.id}"
else
redirect_to @post
end
【问题讨论】:
标签: ruby-on-rails anchor