DevanB
DevanB

Reputation: 377

link_to options

So I'm trying to make this into a link_to link:

<a class="dropdown-toggle" data-toggle="dropdown" href="#">Sign-In <b class="caret"></b></a>

I have this so far, but can't figure out how to handle the <b class="caret"></b>. I have tried a do block at the end of the link_to, but only got an error.

<%= link_to "Sign-In", new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"} %>

I've just started learning Rails, and have searched around (including reading the RoR API docs) but have got nothing to work. Thanks for the help!

Upvotes: 16

Views: 49658

Answers (4)

Awais Shafqat
Awais Shafqat

Reputation: 546

It must be a data attribute in link_to link_to remove_user_path(recipient_id: recipient.id) , method: :delete, data: {confirm: 'Are you sure you want to delete?'}

Upvotes: 0

sjain
sjain

Reputation: 23344

The link_to that you provided is alright in terms of context.

Let me tell you what it is upto:

This is the link that you mentioned:

<%= link_to "Sign-In", new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"} %>

This is the corresponding html link formed by the above link_to:

<a href="users/sessions/new" class="dropdown-toggle" data-toggle="dropdown">Sign-In</a>

Now consider this link:

<%= link_to(raw("Sign-In" +("<b class= 'caret'></b>")), new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"}) %>

The above link will give you the following html link:

<a class="dropdown-toggle" data-toggle="dropdown" href="users/sessions/new">Sign-In<b class= 'caret'></b></a>

Now you are almost close to your result. The only thing is the href.

Ok let's modify the above link_to a bit with just a change in href path as-

<%= link_to(raw("Sign-In" +("<b class= 'caret'></b>")), "#", :class => "dropdown-toggle", :data => {:toggle=>"dropdown"}) %>

The generated link is now equivalent to what you wanted:

<a href="#" class="dropdown-toggle" data-toggle="dropdown">Sign-In<b class= 'caret'></b></a>

Upvotes: 38

ggcodes
ggcodes

Reputation: 2909

put the link in a container it could be a div and put a class ="dropdown".

 <div class="dropdown">
    <%= link_to image_tag('icons/lock_icon.png') + " Log In" +('<b class="caret"></b>').html_safe , '#', { :class => 'dropdown-toggle', 'data-toggle' =>  'dropdown' } %>
 </div>

Upvotes: 0

darph
darph

Reputation: 198

If you provide a block, you omit the first argument (normally the content of the a tag) and it will be replaced by what the block yields.

<%= link_to new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"} do %>
  Sign-In <b class="caret"></b>
<% end %>

Upvotes: 11

Related Questions