link_to options

link_to options

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

I have this so far, but can’t figure out how to handle the . 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!


Solution 1:

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.

Related:  X-editable custom field type not respecting overridden defaults

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>

Solution 2:

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 %>

Solution 3:

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' } %>

Solution 4:

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


Related:  How to disable past dates from today in bootstrap datetimepicker? [duplicate]