简体   繁体   English

rails link_to和button_to与bootstrap按钮类的行为有所不同

[英]rails link_to and button_to with bootstrap button class behaves differently

I've following link_to on show article view page, 我在show article view页面上关注link_to,

<%= link_to "Add Pictures",
      new_picture_path(article_id: @article.id),
      class: "btn btn-small btn-success" %>

This works perfectly by displaying 'link' as a button with the help of "btn" class. 通过在“btn”类的帮助下将“链接”显示为按钮,这非常有效。 Only problem with this is that the text on the button changes to gray after a click (as in visited link visited). 唯一的问题是,单击后按钮上的文本会变为灰色(如访问过的访问链接)。 How do I keep it as original text color (white in this case)? 如何将其保留为原始文本颜色(本例中为白色)? Or what kinds of css magic do I need to keep the original text color. 或者我需要什么样的CSS魔法来保持原始文本颜色。

Or simply I can fix it by changing it to button_to as follows, 或者我可以通过将其更改为button_to来修复它,如下所示,

<%= button_to "Add Pictures",
      new_picture_path(article_id: @article.id),
      method: :get, class: "btn btn-small btn-success" %>

But the problem with this is that, my article_id is get sets to nil, which fails the validation error that article_id is not set. 但问题是,我的article_id是将set设置为nil,这使得未设置article_id的验证错误失败。
What do I do? 我该怎么办? Fix the link_to with css (how?) or fix the button_to issue (how?). 使用css修复link_to(如何?)或修复button_to问题(如何?)。 Any help is appreciated. 任何帮助表示赞赏。

If your css code contains: 如果您的css代码包含:

a:visited {
  color: #666666;
}

This may cause a different rendering between link_to and button_to because link_to will be parsed as <a href=""></a> and button_to will be parsed as <form>..</form> . 这可能会导致link_tobutton_to之间的渲染不同,因为link_to将被解析为<a href=""></a>button_to将被解析为<form>..</form>

Note: if you use 注意:如果您使用

<%= button_to 'Users', users_path(app_id: application.id), method: :get, class: 'btn btn-info btn-xs' %>

Rails will compile as: Rails将编译为:

<form class="button_to" method="get" action="/users?app_id=1">
   <input class="btn btn-info btn-xs" type="submit" value="Users">
</form>

But the app_id=1 will not be passed to params in rails after you click on the button. 但是,单击按钮后, app_id=1将不会传递给rails中的params

The solutions are : 解决方案是:

  1. Use link_to with class attribute: style: "color:white" just like @Rahul says. 使用带有class属性的link_tostyle: "color:white"就像@Rahul所说的那样。

  2. Remove the css code: a:visited 删除css代码: a:visited

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM