【问题标题】:Using "link_to" inside other HTML tags在其他 HTML 标记中使用“link_to”
【发布时间】:2013-08-22 19:57:28
【问题描述】:

我使用的是 Bootstrap 3,我可以做大多数链接,但是当“link_to”函数包含在其他东西中时会出现问题。

这些工作

<li><%= link_to "Contact", contact_path %></li>
<li><%= link_to "Sign Up", register_path %></li>

我不知道如何为这些使用“link_to”

<a class="navbar-brand" href="#">Project</a>

项目的路径是root_path

 <a href="#" class="dropdown-toggle" data-toggle="dropdown">Resources<b class="caret"></b></a>

资源的路径是resources_path

【问题讨论】:

    标签: html css ruby-on-rails ruby


    【解决方案1】:

    你可以这样做:

    <%= link_to "Project", root_path , class: 'navbar-brand' %>
    <%= link_to resources_path, class: 'dropdown-toggle', data: {toggle:'dropdown'} do %>
      Resources <b class="caret"></b>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      您不必必须使用 link_to 帮助程序,以防它实际上对您没有帮助,但您可以使用帮助程序轻松创建所需的链接,如下所示:

      link_to 'Project', '#', class: 'navbar-brand'
      link_to '#', class: 'dropdown-toggle', data: { toggle: 'dropdown' } do
        Resources <b class="caret"></b>
      end
      

      【讨论】:

      • 如果您在 link_to 文本的正文中有另一个链接标签怎么办。 一堆带有 link 的其他文本
      猜你喜欢
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2014-05-07
      • 2013-03-11
      • 2020-03-09
      • 2022-01-14
      相关资源
      最近更新 更多