开发者

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开发者_StackOverflow社区 request.path =~ /\/my_controller/ ) %>


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.


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 => ...} %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜