How to add data-some thing with link_to method

The question is how to create this:

<a class="btn" data-toggle="modal" href="#myModal" >Launch Modal</a>

Using link_to method

<%= link_to “Launch Modal”,"#myModal", :data => {:toggle=>“modal”}, :class=>“btn”%>

Thanks a lot!

<li class="active"><a href="#">Link <span
class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>

<li class="active"><a href="#">Link <span class="sr-only">(current</span
</a></li><li><a href="#">Link</a></li>

<li class="active"><a href="#">Link <span class="sr-only">(current</span
</a></li><li><a href="#">Link</a></li>

<li class="active"><a href="#">Link <span
class="sr-only">(current</span> </a></li><li><a href="#">Link</a></li>

Are you trying to ask a question? If so it is not at all clear what
the question is. I suggest asking the question in the form of a
sentence explaining what you are trying to do.

Colin

Am trying to rewrite this html (<li class="active"><a href="#">Link
<span

class="sr-only">(current)</span></a></li> <li><a href="#">Link</a></li>) into

ruby syntax

Thanx

The question is how to create this:

<li class="active"><a href="#">Link
<span> class="sr-only">(current)</span></a></li> <li><a href="#">Link</a
</li>)

Using link_to method

Am trying to rewrite this html (<li class="active"><a href="#">Link
<span

class="sr-only">(current)</span></a></li> <li><a href="#">Link</a></li>) into

ruby syntax

<%= content_tag :li, class: 'active' do %>
  <%= link_to '#' do %>
    Link
    <%= content_tag :span, '(current)', class: 'sr-only' %>
  <%- end -%>
<%- end -%>
<%= content_tag :li, link_to('Link', '#') %>

Assuming by ruby you mean ERB syntax...

Walter

Thank you

How to create this:

<a data-toggle="dropdown" role="button"
aria-expanded="false">Groups<span class="caret"></span> <a/>

Using link_to method

Please do yourself a favor and visit the Rails Guides site: http://guides.rubyonrails.com and read them. All of this is documented well.

To add a data attribute in ERB, you make a nested hash with data as the outermost key:

<%= link_to( nil, data: { data_toggle: 'dropdown' }, role: 'button', 'aria-expanded': 'false' ) do %>
  Groups
  <span class="caret"></span>
<%- end %>

You didn't ask about aria-expanded, but that's how you would add that as well (Ruby 2.3 syntax, don't try this on 1.9.x). The point of using data as a hash is that you often have many different data-attributes on the same element, so you would do data: { foo: 'bar', baz: 'boo' } and end up with data-foo="bar" data-baz="boo" in the generated HTML.

Walter

Sorry, this should read data: { toggle: 'dropdown' } -- typo on my part.

Walter

You can pass a block to `#link_to`. An example from the docs:

<%= link_to(@profile) do %>
  <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
<% end %>
# => <a href="/profiles/1">
       <strong>David</strong> -- <span>Check it out!</span>
     </a>

See
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to