将类添加到 link_to 会破坏链接
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
因为 :class 应该在 :html_options 中(参考 API)
try this
Since the :class should be in :html_options (refering to API)
正确的做法如下:
至于像这样手动设置控制器和操作,嗯,这是垃圾。 Rails 为您构建 url 助手;使用它们可以为自己节省一些时间、精力,并同时增加清晰度:
您使用字符串插值所做的事情也是一个坏主意……它只是毫无理由地浪费和混乱。以下是相同的,只是更好:
至于为什么你的链接不起作用,如果将其包装在 div 中有帮助,听起来你的 HTML 结构有问题,而不是 link_to 语法。
The proper way of doing what you have is as follows:
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:
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:
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.
我正在使用 link_to do-end 块,因此上述先前的解决方案对我不起作用。
如果您想在 a 标记中嵌入其他标记,则可以使用 link_to 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.
In this case it's
请小心,因为在 Rails 5 中上述方法仍然会导致错误的 URL 生成。控制器和操作需要放入字面散列中才能在 Rails 5 中工作。您将拥有的应该是这样的
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