【问题标题】:rails links_to helper with html block带有 html 块的 rails link_to 助手
【发布时间】:2016-02-19 12:37:25
【问题描述】:

我有一个 rails 4.2 应用程序。我想让整个tr 可以点击并将用户带到用户到产品展示页面。

目前,html 在没有 <href.. 的情况下呈现,我不完全明白为什么。我错过了什么?

index.html.erb

<div class="panel panel-default">
  <table class="table">
    <tbody class="product-profile-index">
      <%= render @products %>
    </tbody>
  </table>
</div>

_product.html.erb(部分用于索引页)

<%= link_to product do %>
  <tr class = "product-index-row">
    <td class="col-md-3 name">
      <span class="product-image">
        <%= image_tag attachment_url(product, :product_image, :fill, 45, 45) %>
      </span>
      <span class="product-name">
        <%= product.name %>
      </span>
    </td>
    <td class="col-md-6 oneliner">
      <%= product.oneliner %>
    </td>
    <td class="col-md-3 updated">
      <%= local_time_ago(product.updated_at) %>
    </td>
   </tr>
<% end %>

css.scss

.product-profile-index {
  .product-index-row {
    &:hover {
      background-color: $gray-medium-light;
    }
  }
}

更新:

如果我像这样只包装 html 的一小部分,那么它就可以工作了:

<%=link_to product %>
  <%= product.name%>
<% end %>

【问题讨论】:

  • 您能在页面某处添加&lt;%= product_path(@product) %&gt; 并告诉我们它输出的内容吗?
  • 它工作正常。我曾经用&lt;%=link_to product %&gt;&lt;%= product.name%&gt;&lt;% end %&gt; 使用它并且工作正常。 (我只是将实例变量放在我的问题中以使其更易于阅读,但这不是问题)
  • 优秀。你可以添加它以便我们看到吗?

标签: html css ruby-on-rails erb link-to


【解决方案1】:

尝试使用此代码

<tr class = "product-index-row" data-href"/product/<%=@product.id%>"> 

【讨论】:

    【解决方案2】:

    我找到了解决方案。它不是 DRY,但效果很好。

    我必须像这样将链接传递给每个td

    .......
    <td>
    <%= link_to(product, html_options = {style:"display:block;"}) do %>
    ......
    <% end %>
    </td>
    ..........
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      • 2013-05-13
      相关资源
      最近更新 更多