adding a class to a link_to is breaking the link

mtay picture mtay · Apr 18, 2011 · Viewed 84.5k times · Source

I'm using link_to in RoR 3

When I use it like this, it works fine:

<%= link_to "Add to your favorites list",:controller => 
            'favourite_companies', :action =>'create', 
            :company_id=>"#{@company.id}",   
            :company_name=>"#{@company.company_name}" %>

But I would like to pass in a class as well

however, this is not working for me. The class works, but it breaks the link. Any ideas?

<%= link_to "Add to your favorites list",{:controller => 
            'favourite_companies', :action =>'create'}, 
            :company_id=>"#{@company.id}",   
            :company_name=>"#{@company.company_name}",
            :class=>"ui-button-text button_text"}  %>

Answer

edthix picture edthix · Apr 18, 2011
<%= link_to "Add to your favorites list",{:controller => 
            'favourite_companies', :action =>'create'}, 
            :company_id=>"#{@company.id}",   
            :company_name=>"#{@company.company_name}",
            :class=>"ui-button-text button_text"}  %>

try this

<%= link_to "Add to your favorites list", :controller => 
            'favourite_companies', :action =>'create', 
            :company_id=>"#{@company.id}",   
            :company_name=>"#{@company.company_name}",
            { :class=>"ui-button-text button_text" }  %>

Since the :class should be in :html_options (refering to API)

link_to(body, url, html_options = {})