Ruby-on-rails 将类添加到 link_to 会破坏链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5697512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:57:14  来源:igfitidea点击:

adding a class to a link_to is breaking the link

ruby-on-railslink-to

提问by mtay

I'm using link_to in RoR 3

我在 RoR 3 中使用了 link_to

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

回答by edthix

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

由于 :class 应该在 :html_options 中(参考 API)

link_to(body, url, html_options = {})

回答by coreyward

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:

至于像这样手动设置控制器和动作,好吧,这是废话。Rails 为你构建了 url helper;使用它们,同时节省一些时间、精力并增加清晰度:

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.

至于为什么你的链接不起作用,如果将它包装在一个 div 中,听起来你的 HTML 结构有问题,而不是 link_to 语法。

回答by Chris

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

我正在使用 link_to do-end 块,因此上述以前的解决方案对我不起作用。

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

如果你想在你的 a 标签中嵌入其他标签,那么你可以使用 link_to do-end 块。

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

回答by Kenigbolo

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

小心,因为在 Rails 5 中,上述方法仍然会导致错误的 URL 生成。控制器和动作需要放在一个文字散列中才能在 Rails 5 中工作。 你将拥有的应该是这样的

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