Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 972
DevanB Avatar asked Feb 27 '13 06:02

DevanB


Video Answer


2 Answers

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>
like image 180
My God Avatar answered Sep 24 '22 16:09

My God


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 %>
like image 35
darph Avatar answered Sep 24 '22 16:09

darph