Rails link_to and HTML options
Just a small thing, but I needed to know how to do this and it took me a while to find. Hopefully this will help someone else out.
In HTML, we might declare a link like so
<a title="Example" href="/controller/action">Click Here!</a>
Now, in Ruby on Rails, we want to declare a link, so we use the built in link_to method like this
<%= link_to "Click Here!", :controller => "controller", :action => "action" %>
Hmmm… how do we squeeze a title in there? Luckily, the third parameter to link_to is a hash of HTML options.
<%= link_to "Click Here!", {:controller => "controller", :action => "action"}, {:title => "Example"} %>
That should do it. I feel like I still have so much to learn about Ruby on Rails, but the more I learn the more I want to learn!
More of my rantings
- Quick Tip: Override Rails Generated URLs
- Quick Tip: Rails Named Bind Variables
- Ruby on Rails Testing – login gotcha
These might also interest you
- Photographer in Red and Blue (Brainfuel)
- Merb, Rails Myths, Language Popularity and other Zenbits (Antonio Cangiano)
- Wink and a Flash (Brainfuel)
ah cool that you figured it out. Just an fyi, in many if not all of the ruby method calls which autogenerate html for you, it is possible to pass in any html parameter through what you just did.
So if you know that the ruby call generates a textArea for you and you want to override the max_size or whatever (i don’t know if that’s true), you do it via the parameters. This completely tripped me up for such a long time because I was looking at the ruby api expecting an enumeration of which html attribute I could override until I realized you can pass any in.
October 27th, 2006 at 8:45 pmvery helpful, thanks!!
January 16th, 2010 at 9:45 pmthanks! saved me time searching
March 25th, 2010 at 11:19 pm