如何在 Ruby on Rails 中创建一个锚点并重定向到这个特定的锚点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/757891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an anchor and redirect to this specific anchor in Ruby on Rails
提问by Damian
I'm trying to create unique anchors for every comment on my blog so a person can take the url of an anchor and paste it in their browser, which will automatically load the page and scroll down to the point in the page where their comment starts.
我正在尝试为我博客上的每条评论创建独特的锚点,以便人们可以获取锚点的 url 并将其粘贴到他们的浏览器中,这将自动加载页面并向下滚动到页面中他们评论开始的位置.
Perhaps I'm going about this the wrong way but I've tried this which was to no avail.
也许我以错误的方式解决这个问题,但我已经尝试过,但无济于事。
Comment view - Fail 1 - when pasted in a browser this link does not scroll down to the desired position
评论视图 - 失败 1 - 粘贴到浏览器中时,此链接不会向下滚动到所需位置
<%= link_to '#', :controller => 'posts', :action => 'show', :id => comment.post, :anchor => 'comment_' << comment.id.to_s %>
Comments controller - Fail 2 - Correct url in browser but no scrolling happens it just stays at the top of the page
评论控制器 - 失败 2 - 在浏览器中正确的 url 但没有滚动发生它只是停留在页面的顶部
redirect_to :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_' + @comment.id.to_s
If someone could help I'd be very grateful :)
如果有人可以提供帮助,我将不胜感激:)
UPDATE: The solutions below almost work, however I come out with the following URL which isn't being scrolled to if I click on it.
更新:下面的解决方案几乎可以工作,但是我得到了以下 URL,如果我点击它,它不会被滚动到。
采纳答案by vrish88
It looks like you want to use the link_tocode that you have in your question. Then in your list of comments you have to make sure that you have an anchor tag named the same thing in the link.
看起来您想使用问题中的link_to代码。然后在您的评论列表中,您必须确保在链接中有一个名为相同内容的锚标记。
So this:
所以这:
<%= link_to 'Your comment', post_path(@comment.post) + "#comment_#{@comment.id.to_s}" %>
will generate something like this
会产生这样的东西
<a href="localhost:3000/posts/2#1comment_234">Your comment</a>
/* html code */
<a name="comment_1234">This is a comment</a>
You have to manually tack on the #comment_otherwise the link_to method thinks that the :anchor attribute that you are passing it is for that tag.
您必须手动添加,#comment_否则 link_to 方法认为您传递的 :anchor 属性是针对该标签的。
回答by XGamerX
Actually, anchor is an option for the path, not for the link_to
实际上,anchor 是路径的一个选项,而不是 link_to
<%= link_to '#', post_path(comment.post, :anchor => "comment_#{comment.id}") %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001565
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001565
link_to "Comment wall", profile_path(@profile, :anchor => "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
回答by Mohamad
Here's an improvement on @XGamerX's answer.
这是对@XGamerX 答案的改进。
<%= link_to '#', [comment.post, { anchor: dom_id(comment) }] %>
Or
或者
<%= link_to '#', post_path(comment.post, anchor: dom_id(comment)) %>
回答by nilid
this is best way:
这是最好的方法:
<%= link_to '#', post_path(comment.post, anchor: dom_id(comment.id)) %>
回答by John Topley
Try this:
尝试这个:
<%= link_to '#', post_path(comment.post), :anchor => "comment_#{comment.id}" %>
回答by klew
These links will scroll down to position where you have code like:
这些链接将向下滚动到您有代码的位置,例如:
<a name="comment_1"></a>
I don't know if there are helpers that will do it for you, but it is very simple and you can write your own.
我不知道有没有帮手会为你做,但它很简单,你可以自己写。

