我在 posts/index 视图上使用 link_to 标签,并希望使用锚点将其链接到 posts/show/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' %>
and
<%= link_to 'Add a Comment', :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_form' %>
但我没有运气。
这是我的帖子#show action:
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 中。由于某种原因,尝试链接到显示页面时它不起作用。有什么帮助吗?