adding a class to a link_to is breaking the link

≯℡__Kan透↙ 提交于 2019-12-03 05:35:55

问题


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

回答1:


<%= 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 = {})



回答2:


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:


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:


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


来源:https://stackoverflow.com/questions/5697512/adding-a-class-to-a-link-to-is-breaking-the-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!