How to add data-some thing with link_to method

49 views
Skip to first unread message

Karthikeyan A k

unread,
Mar 27, 2012, 10:16:35 PM3/27/12
to rubyonra...@googlegroups.com
The question is how to create this:

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

Using link_to method

--
Posted via http://www.ruby-forum.com/.

Javier Quarite

unread,
Mar 27, 2012, 10:19:12 PM3/27/12
to rubyonra...@googlegroups.com
On Tue, Mar 27, 2012 at 9:16 PM, Karthikeyan A k <li...@ruby-forum.com> wrote:
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"%> 

Karthikeyan A k

unread,
Mar 29, 2012, 6:04:05 AM3/29/12
to rubyonra...@googlegroups.com
Thanks a lot!

Nana Kwame

unread,
Jun 6, 2016, 9:45:14 AM6/6/16
to rubyonra...@googlegroups.com
<li class="active"><a href="#">Link <span
class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>

Nana Kwame

unread,
Jun 6, 2016, 9:45:38 AM6/6/16
to rubyonra...@googlegroups.com
<li class="active"><a href="#">Link <span class="sr-only">(current</span
</a></li><li><a href="#">Link</a></li>

Nana Kwame

unread,
Jun 6, 2016, 9:46:03 AM6/6/16
to rubyonra...@googlegroups.com
<li class="active"><a href="#">Link <span class="sr-only">(current</span
</a></li><li><a href="#">Link</a></li>

Nana Kwame

unread,
Jun 6, 2016, 9:47:40 AM6/6/16
to rubyonra...@googlegroups.com
<li class="active"><a href="#">Link <span
class="sr-only">(current</span> </a></li><li><a href="#">Link</a></li>

Colin Law

unread,
Jun 6, 2016, 9:49:21 AM6/6/16
to Ruby on Rails: Talk
On 6 June 2016 at 14:44, Nana Kwame <li...@ruby-forum.com> wrote:
> <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

Nana Kwame

unread,
Jun 6, 2016, 5:06:05 PM6/6/16
to rubyonra...@googlegroups.com
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

Nana Kwame

unread,
Jun 6, 2016, 5:09:49 PM6/6/16
to rubyonra...@googlegroups.com
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>)

Walter Lee Davis

unread,
Jun 6, 2016, 8:39:53 PM6/6/16
to rubyonra...@googlegroups.com

> On Jun 6, 2016, at 5:04 PM, Nana Kwame <li...@ruby-forum.com> wrote:
>
> 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

>

> Thanx
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bff1ec47f70b7309d4b94b6c9d435d38%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Nana Kwame

unread,
Jun 7, 2016, 6:07:54 AM6/7/16
to rubyonra...@googlegroups.com
Thank you

Nana Kwame

unread,
Jun 7, 2016, 6:18:35 PM6/7/16
to rubyonra...@googlegroups.com
How to create this:


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

Walter Lee Davis

unread,
Jun 7, 2016, 6:31:01 PM6/7/16
to rubyonra...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/54fd5772eebc2f8f2d02cc6d50110fee%40ruby-forum.com.

Walter Lee Davis

unread,
Jun 7, 2016, 6:40:01 PM6/7/16
to rubyonra...@googlegroups.com
Sorry, this should read data: { toggle: 'dropdown' } -- typo on my part.

Walter

> On Jun 7, 2016, at 6:30 PM, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
> data: { data_toggle: 'dropdown' },

Greg Navis

unread,
Jun 8, 2016, 12:30:15 AM6/8/16
to rubyonra...@googlegroups.com
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>

--
Greg Navis
I help tech companies to scale Heroku-hosted Rails apps.

Reply all
Reply to author
Forward
0 new messages