Rails link_to and HTML options

Posted by Curtis Miller Curtis Miller

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!



Velocity Labs

Need web application development, maintenance for your existing app, or a third party code review?

Velocity Labs can help.

Hire us!