【问题标题】:Link to an Anchor on page in Rails App在 Rails 应用程序中链接到页面上的锚点
【发布时间】:2016-01-26 10:48:43
【问题描述】:

现在我的导航栏中有这个链接。但是一旦我将东西移到 Rails 应用程序中,就没有工作了。

<li><a href="products.html#ultra">Ultra</a></li>

我明白需要改成

<li><%= link_to "Ultra", product_path %></li>

我希望它直接转到我在产品页面上设置的锚点。这在 Rails 中是如何完成的?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    url helper 方法可以将anchor 哈希作为参数,并将其作为锚链接输出:

    &lt;li&gt;&lt;%= link_to "Ultra", product_path(anchor: 'ultra') %&gt;&lt;/li&gt;

    来自the docs

    link_to 还可以生成带有锚点或查询字符串的链接:

    link_to "Comment wall", profile_path(@profile, anchor: "wall")
    # => <a href="/profiles/1#wall">Comment wall</a>
    

    【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2014-05-24
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    相关资源
    最近更新 更多