我有一个博客应用程序,其帖子可以通过vote帖子控制器中的操作接收投票.因为我允许在索引和显示视图中redirect_to :back进行投票,所以我在投票后进行投票,如下所示.
def vote
# voting logic goes here
redirect_to :back
end
Run Code Online (Sandbox Code Playgroud)
这会将我重定向到正确的页面,但我想重定向到页面中的特定帖子.为此,我在帖子部分div中添加了一个识别锚点.
<div id="post_<%= post.id %>">
<%= post.content %>
</div>
Run Code Online (Sandbox Code Playgroud)
我怎么能在我的网站上引用这个redirect_to :back?我尝试了以下,这是行不通的.
# this doesn't work!
redirect_to :back, anchor: "post_#{@post.id}"
Run Code Online (Sandbox Code Playgroud)
或者,如果我对show和index视图使用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
Run Code Online (Sandbox Code Playgroud)