Rails 3.1.3 使用带有 link_to 标记的锚属性从 posts/index 到 posts/show/id 不起作用
我在 posts/index 视图上使用 link_to 标签,并希望使用锚点将其链接到 posts/show/id 视图,使其向下滚动到评论表单。由于某种原因,我无法让锚点工作。这是我的代码:
在 posts/index
<%= 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' %>
但我没有运气。
这是我的 posts#show 操作:
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
这是我希望锚点滚动到的 posts/show 视图:
<h2><a name="comment_form" id="comment_form">Add a comment:</a></h2>
此外,如果我链接到索引页面上的某些内容,则上述任何操作都有效,因为我可以看到哈希#已附加到输出的 url 中。由于某种原因,尝试链接到显示页面时它不起作用。有什么帮助吗?
I am using a link_to tag on my posts/index view and want to link it to my posts/show/id view with an anchor that makes it scroll down to the comments form. For some reason I can't get the anchor to work. Here is my code:
In posts/index
<%= link_to 'Add a Comment', post, :anchor => 'comment_form' %>
This fails to append the # sign to the end of the link, so it is just localhost:3000/posts/id.
I have also tried many variations for link_to, including:
<%= link_to 'Add a Comment', post(:anchor => 'comment_form' %>
and
<%= link_to 'Add a Comment', :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_form' %>
but I've had no luck.
Here is my posts#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
and here is the posts/show view where I want the anchor to scroll to:
<h2><a name="comment_form" id="comment_form">Add a comment:</a></h2>
Furthermore, any of the above works if I am linking to something on the index page, as I can see the hash # has been appended to the outputted url. For some reason it is not working when trying to link to the show page. Any help with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
link_to
的第二个参数通常按原样传递给url_for
,第三个参数用作的属性哈希。最终生成的
元素。因此,在第一个示例中,您传递一个
Post
对象作为第二个参数,传递一个哈希作为第三个参数。只有Post
会传递给url_for
。它永远看不到包含:anchor
选项的哈希,因此您不会在生成的 URL 末尾看到锚点。 (但是您可能会在生成的元素上看到
anchor="comment_form"
属性。)您的第二个示例在语法上不正确。我想这会导致错误。
你的第三个例子......应该有效。我不知道为什么没有:-)
Try this:
The second argument to
link_to
is typically passed as-is tourl_for
, and the third argument is used as an attributes hash for the<a>
element that ultimately gets generated.So in your first example, you're passing a
Post
object as the second argument and a hash as the third argument. Only thePost
would be passed tourl_for
. It never sees the hash containing the:anchor
option, so you wouldn't see the anchor at the end of the generated URL. (But you would probably see ananchor="comment_form"
attribute on the generated<a>
element.)Your second example is syntactically incorrect. I imagine that resulted in an error.
Your third example...should've worked. I'm not sure why it didn't :-)