7

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/ ) %>

2 Answers 2

13

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.

7

In your code, :class is being included in the url_options hash rather than html_options. Try something like this:

<%= link_to "link", {:controller => :my_controller, :action => :index}, {:class => ...} %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.