【问题标题】:Nested html with content_tag带有 content_tag 的嵌套 html
【发布时间】:2016-04-15 19:26:23
【问题描述】:

我想使用 content_tag 或 content_tag_for 获取这个 html:

<a href="javascript:;" class="nav-link ">
  <%= fa_icon 'book', 'aria-hidden': true %>
  <span class="title">Courses</span>
</a>

我可以将 fa_icon 放在 link_to 中,但不知道如何放置 span。如果它不可行,如果我这样做怎么办:

<a href="<%=courses_path%>" class="nav-link ">
  <%= fa_icon 'book', 'aria-hidden': true %>
  <span class="title">Courses</span>
</a>

【问题讨论】:

    标签: ruby-on-rails actionview content-tag


    【解决方案1】:

    为什么不如下?:

    <%= link_to "javascript:;", class: "nav-link" do %>
      <%= fa_icon 'book', 'aria-hidden': true %>
      <%= content_tag :span, "Courses", class: "title" %>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      我会尝试link_to ... do。因为它是一种比content_tag 更具描述性的标准方法,我认为当您在两年后回来进行维护时会更容易理解代码的初衷。

      这是你的 sn-p 的样子,让你开始:

      <%= link_to courses_path, class: 'nav-link' do %>
        <%= fa_icon 'book', 'aria-hidden': true %>
        <span class="title">Courses</span>
      <% end %>
      

      这是 Rails 4.2 的文档:http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 1970-01-01
        • 2012-03-27
        • 1970-01-01
        • 2015-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多