【问题标题】:Converting a complex html.erb anchor to haml将复杂的 html.erb 锚转换为 haml
【发布时间】:2010-11-17 16:08:10
【问题描述】:

我尝试将以下代码转换为 HAML。在锚点之前一切都很容易。

<% @user ||= current_user %>
<div class="stats">
  <table summary="User stats">
    <tr>
      <td>
        <a href="<%= following_user_path(@user) %>">
          <span id="following" class="stat">
            <%= @user.following.count %> following
          </span>
        </a>

我很接近:

- @user ||= current_user
.stats
  %table{ :summary => "User stats" }
    %tr
      %td
        %a
          = following_user_path(@user)
          %span.stat#following
            = @user.following.count
            following

但锚并不完全正确。 我相信我应该能够使用 link_to 来做到这一点,但我不清楚如何在 link_to 的参数中混合嵌入的 SPAN 标签。这应该怎么做? 感谢您的帮助。
汤姆

【问题讨论】:

    标签: ruby-on-rails haml


    【解决方案1】:

    您可以使用link_to 助手:

    = link_to following_user_path(@user) do
      %span#following.stat
        == #{@user.following.count} following
    

    【讨论】:

      【解决方案2】:

      我认为你可以使用:

          %a{ :href => following_user_path(@user) }
            %span.stat#following
              = @user.following.count
              following
      

      【讨论】:

      • 很好,谢谢。我也想了解是否有办法使用 link_to 来做到这一点。项目的复杂性是否让人们无法使用 link_to?
      • 是的,你可以。 link_to 方法可以接收可选的散列参数,在那里你可以设置锚元素的类、id 和其他属性。类似于:link_to 'Link', '#', :id => 'element_id', :class=> 'element_class'
      猜你喜欢
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多