代码之家  ›  专栏  ›  技术社区  ›  NullVoxPopuli

rubyonrails:如何在link-to-helper中添加类?

  •  7
  • NullVoxPopuli  · 技术社区  · 16 年前

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

    这是我的密码

    <%= link_to "link", :controller => :my_controller, :action => :index, :class=>("on" if request.path =~ /\/my_controller/ ) %>
    
    2 回复  |  直到 16 年前
        1
  •  13
  •   Eric    13 年前

    如果你读了 API ,您将看到以下示例:

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

    这意味着link-to的语法是“link to something,then something in braces,then another thing in braces”。另一种解释方法是,块必须是散列。

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

    如果你愿意的话可以放在一行。

        2
  •  7
  •   user94896 user94896    16 年前

    在你的代码里, :class url_options 散列而不是 html_options . 尝试以下操作:

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