adding a class to a link_to is breaking the link

2020-05-18 06:06发布

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"}  %>

4条回答
老娘就宠你
2楼-- · 2020-05-18 06:24

The proper way of doing what you have is as follows:

link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"

As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:

link_to "Foo", favourite_companies_path(@company), :method => :post

What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:

link_to "Foo", :company_id => @company.id, :company_name => @company.name

As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.

查看更多
家丑人穷心不美
3楼-- · 2020-05-18 06:28

I'm using a link_to do-end block so the above previous solutions didn't work for me.

If you want to embed other tags in your a tag, then you can use the link_to do-end block.

<%= link_to favourite_companies_path(:company_id => @company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
  <i class="fa fa-star"></i>
  <%= @company.company_name %>
<% end %>

In this case it's

<%= link_to path(url_params), html_options = {} do %>
<% end %>
查看更多
神经病院院长
4楼-- · 2020-05-18 06:37
<%= 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 = {})
查看更多
不美不萌又怎样
5楼-- · 2020-05-18 06:46

Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like 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"   %>
查看更多
登录 后发表回答