简体   繁体   English

带有HTML块的rails links_to helper

[英]rails links_to helper with html block

I have a rails 4.2 app. 我有一个Rails 4.2应用程序。 I would like to have the whole tr to be clickable and take the user to the user to the product show page. 我希望整个tr都是可单击的,并将用户带到产品展示页面的用户。

At the moment the html gets rendered without <href.. and I don't exactly understand why. 目前,该html呈现时没有<href..而且我还不完全理解为什么。 What did I miss? 我错过了什么?

index.html.erb index.html.erb

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

_product.html.erb (partial for index page) _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 css.scss

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

UPDATE: 更新:

If I wrap just a smaller part of html like this, then it's working: 如果我像这样包装html的一小部分,则可以正常工作:

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

尝试使用此代码

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

I found a solution. 我找到了解决方案。 It's not DRY but working pretty well. 它不是干的,但是运行得很好。

I had to pass the link into each td like this: 我必须像这样将链接传递到每个td

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM