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!

Posted October 27th, 2006 at 8:45 pm in Ruby on Rails | Permalink

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

3 comments:

  1. dan:

    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.

  2. ruby_newbie:

    very helpful, thanks!!

  3. Yuvi:

    thanks! saved me time searching :)

Leave a response: