简体   繁体   中英

Rails HAML div as link -> not getting it worked

I try to make a complete DIV as a link, but it is just working. 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:

<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. 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. 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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