Suraj
Suraj

Reputation: 2563

anchor tag not working when it has span inside

Why I can not have this as an achor tag?

Why the below code is not working.

I have p which has a, a has span and a closes after span

Here's the code. What's wrong?

      <p class="paraClass"> 
        <a>
         <span id="cartitems">
         <%= pluralize(@size, 'item') %>     
         </span>  
        </a>
      </p> 

Please help

Upvotes: 2

Views: 1667

Answers (3)

Tim Kretschmer
Tim Kretschmer

Reputation: 2280

An A-Tag needs to have a href - else its just a anchor.

A Good way is to use a helper of rails called link_to You can use it in two ways, just pass content-string and url, or pass url and a block. If you pass a block, the return of the block will be used as the display of the a-tag.

link_to "content", "url", {options}

link_to "url", {options} do block end

Unless you're just using some string as the display-text, i always recommend you to use the block-style. It is easier for you to edit something inside of that block and of course its easier to read and understand.

In your case it would be:

<p class="paraClass"> 
    <%= link_to "#", onclick: "return false;" do %>
      <span id"cartitems">
         %= pluralize(@size, 'item') %>    
      </span>
    <%- end %>
</p>

Maybe you want to switch to haml which helps you to write html with ease. In Haml the same code looks sweet within 2 lines of code:

=link_to "#" do
  %span#cartitems=pluralize @size, "item"

Upvotes: 2

Shahzad Barkati
Shahzad Barkati

Reputation: 2526

UPDATE: [ as per comment ]

Its simple. Only add the href tag like below code:

<p class="paraClass"> 
    <a>
     <span id="cartitems">
     <%= pluralize(@size, 'item') %>     
     </span>  
    </a>
  </p>

<!-- here simply changing the color -->
<style>
 a { cursor: pointer; }
 a:hover{ color: #ff9900; }
</style>

Upvotes: 1

Gaurav Mamidwar
Gaurav Mamidwar

Reputation: 338

<%= link_to content_tag(:span, pluralize(@size, 'item') ), '#' %>

use link to tag with content_for... so that it will show u how u want

Upvotes: 0

Related Questions