简体   繁体   English

Rails HAML div作为链接->无法正常工作

[英]Rails HAML div as link -> not getting it worked

I try to make a complete DIV as a link, but it is just working. 我尝试制作一个完整的DIV作为链接,但是它只是起作用。 This is what I have: 这就是我所拥有的:

= link_to (user_orders_path(current_user)) do
  .current_orders.box.tile.one_third.lightblue
    .count
      %i.icon-shopping-cart
      =@current_orders
    .link
      - if @current_orders > 0
        = link_to t('.current_orders'), user_orders_path(current_user)
      - else
        = t('.no_current_orders')

But somehow Rails is making it as: 但是以某种方式Rails使其成为:

<a href="/users/1/orders"></a>
<div id="current_orders" class="box tile one_third lightblue">
  <a href="/users/1/orders">
    <div class="count">
      <i class="icon-shopping-cart"></i>
      3
    </div>
  </a>
  <div class="link">
     <a href="/users/1/orders"> </a>
     <a href="/users/1/orders">Open bestellingen</a>
  </div>
</div>

What am I doing wrong? 我究竟做错了什么? It should be generated as: 它应生成为:

<a href="/users/1/orders">

  <div id="current_orders" class="box tile one_third lightblue">

    <div class="count">
      <i class="icon-shopping-cart"></i>
       3
    </div>

    <div class="link">
       <a href="/users/1/orders">Open bestellingen</a>
    </div>
  </div>
</a>

The first thing I see is that you have a link nested within a link, which will not work. 我看到的第一件事是您有一个嵌套在链接中的链接,该链接不起作用。

It sounds like the behavior you want is a link (1) that is only present if there are current_orders (just show a message if there are not), and (2) where the clickable area is the entire div. 听起来您想要的行为是一个链接(1)仅在存在current_orders时才存在(如果不存在则只显示一条消息),以及(2)其中可单击区域是整个div。 Is this correct? 这个对吗?

If so, (1) use your if statement to conditionally render your div, and (2) place the div inside an '%a' tag like so. 如果是这样,(1)使用if语句有条件地呈现div,并且(2)像这样将div放在'%a'标记内。 Maybe something like this: 也许是这样的:

-if @current_orders > 0
  %a{:href => user_orders_path(current_user)}
    .current_orders.box.tile.one_third.lightblue
      .count
        %i.icon-shopping-cart
        =t('.current_orders')
        =@current_orders
- else
  .current_orders.box.tile.one_third.lightblue
    =t('.no_current_orders')

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

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