将类添加到 link_to 会破坏链接

发布于 2024-11-02 03:24:49 字数 686 浏览 3 评论 0原文

我在 RoR 3 中使用 link_to

当我像这样使用它时,它工作正常:

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

但我也想传递一个类

,但这对我不起作用。该类有效,但它破坏了链接。有什么想法吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巷子口的你 2024-11-09 03:24:49
<%= 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"}  %>

试试这个

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

因为 :class 应该在 :html_options 中(参考 API)

link_to(body, url, html_options = {})
<%= 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 = {})
北方的韩爷 2024-11-09 03:24:49

正确的做法如下:

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

至于像这样手动设置控制器和操作,嗯,这是垃圾。 Rails 为您构建 url 助手;使用它们可以为自己节省一些时间、精力,并同时增加清晰度:

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

您使用字符串插值所做的事情也是一个坏主意……它只是毫无理由地浪费和混乱。以下是相同的,只是更好:

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

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

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.

莫言歌 2024-11-09 03:24:49

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

如果您想在 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 %>

在这种情况下是

<%= link_to path(url_params), html_options = {} do %>
<% end %>

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 %>
空城旧梦 2024-11-09 03:24:49

请小心,因为在 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"   %>

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"   %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文