我正在尝试创建指向特定网页部分ID的链接。
它类似于下面的html,但我想使用rails而不是......
<a id="tips">Useful Tips Section</a>
<a href="#tips">Visit the Useful Tips Section</a>
如何在link_to函数中指定“#tips”?或者我应该创建一个特定的路线?怎么样?
提前致谢
答案 0 :(得分:5)
您可以使用
<%= link_to "Visit the Useful Tips Section", action_path(anchor: tips) %>
在路线中,您可以指定action_path
。
根据Rails API文档,您应该采用上述方法,请参阅this,并在下面找到示例:
link_to "Comment wall", profile_path(@profile, anchor: "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
答案 1 :(得分:0)
尝试使用link_to
<%= link_to "Visit the Useful Tips Section","/#tips" %>
假设您要链接到同一页面上的ID
答案 2 :(得分:0)
<%= link_to('new button', action: 'login' , class: "text-center") %>
为login.html i.g
创建了一个锚标记<a href="login.html" class = "text-center"> new button </a>
和
<a href="admin/login.html" class = "text-center"> new button </a>
使用
<%= link_to('new button', controller: 'admin',
action: 'login' , class: "text-center") %>