Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: how do you add a class in a link_to helper?

how do you get a link_to to generate something like

<a class="myclass".....

this is my code

<%= link_to "link", :controller => :my_controller, :action => :index, :class=>("on" if request.path =~ /\/my_controller/ ) %>
like image 545
NullVoxPopuli Avatar asked Sep 16 '10 23:09

NullVoxPopuli


1 Answers

If you read the API, you'll see the following example:

link_to(body, url_options = {}, html_options = {})

This means the syntax for link_to is "link to something, then something else in braces, then another thing in braces." Another way of interpreting it is that the chunks have to be hashes.

link_to "link",
        { :controller => :my_controller, :action => :index }, 
        { :class=>("on" if request.path =~ /\/my_controller/ ) }

Which can all be placed on one line if you like.

like image 81
Eric Avatar answered Nov 16 '22 02:11

Eric