我在我的帖子/索引视图上使用了一个 link_to 标记,并希望将它链接到我的帖子/显示/id 视图,并使用一个使其向下滚动到评论表单的锚点。由于某种原因,我无法让锚工作。这是我的代码:
在帖子/索引中
<%= link_to 'Add a Comment', post, :anchor => 'comment_form' %>
这无法将 # 符号附加到链接的末尾,因此它只是 localhost:3000/posts/id。我还尝试了许多 link_to 的变体,包括:
<%= link_to 'Add a Comment', post(:anchor => 'comment_form' %>
和
<%= link_to 'Add a Comment', :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_form' %>
但我没有运气。
这是我的帖子#show 操作:
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
这是我希望锚滚动到的帖子/显示视图:
<h2><a name="comment_form" id="comment_form">Add a comment:</a></h2>
此外,如果我链接到索引页面上的某些内容,上述任何一项都有效,因为我可以看到哈希 # 已附加到输出的 url。由于某种原因,它在尝试链接到显示页面时不起作用。有什么帮助吗?