18

How can I use the link_to method in Rails 4 to provide the same effect as

  <a href="(mysite.example.com)" class="tooltips" data-toggle="tooltip" data-placement="top" title="" data-original-title="Facebook">
Zippo9
  • 706
  • 2
  • 5
  • 24

2 Answers2

45
<%= link_to 'my site', 'mysite.example.com', { :class => 'tooltips', 'data-toggle' => 'tooltip', 'data-placement' => 'top', :title => '', 'data-original-title' => 'Facebook' } %>

Or

<%= link_to 'my site', root_path, class: 'tooltips', title: '', data: { toggle: 'tooltip', placement: 'top', original_title: 'Facebook' } %>

Or

<%= link_to 'my site', { controller: 'home', action: 'index' }, { class: 'tooltips', title: '', data: { toggle: 'tooltip', placement: 'top', original_title: 'Facebook' } } %>
makhan
  • 3,809
  • 2
  • 18
  • 24
  • 2
    Thanks I was having a hard time getting the right syntax – Zippo9 Apr 03 '15 at 16:51
  • It's slightly confusing because there are multiple definitions of `link_to` and most of them take two hashes as arguments, which means that just listing options won't work in most cases. Check out Signatures section on http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to – makhan Apr 06 '15 at 04:50
  • Thanks I was definitely struggling with understanding this, will do – Zippo9 Apr 06 '15 at 04:51
  • interesting, when using this in nav-pills, clicking on the links does not do anything when adding in this data-toggle option, it seems to completely disable them for me – Riptyde4 Jan 11 '16 at 09:47
0

A simple working example to copy and edit:

<%= link_to "My hidden stuff", "#collapseArea", { class: "btn btn-light", 'data-toggle': "collapse" } %><br><br>

<div class="collapse" id="collapseArea">
  <p>Hidden</p>
  <p>content</p>
  <p>goes</p>
  <p>here</p>
</div>
stevec
  • 41,291
  • 27
  • 223
  • 311