Ruby-on-rails Rails 将链接转换为引导按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13513457/
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-02 21:03:40  来源:igfitidea点击:

Rails convert links to bootstrap buttons

ruby-on-railstwitter-bootstrap

提问by ardochhigh

I have built some code that sorts an index page, using links in my view. This works fine with links, and the working code is this:

我已经构建了一些代码来对索引页面进行排序,在我的视图中使用链接。这适用于链接,工作代码是这样的:

<p>
  <%= link_to "Sentence", :controller => params[:controller], :action => params[:action], :type => '1', :class => "btn" %>
  <%= link_to "Question", :controller => params[:controller], :action => params[:action], :type => '2', :class => "btn" %>
  <%= link_to "Mnemonic", :controller => params[:controller], :action => params[:action], :type => '3', :class => "btn"  %>
  <%= link_to "Article", :controller => params[:controller], :action => params[:action], :type => '4', :class => "btn"  %>
  <%= link_to "Recommendation", :controller => params[:controller], :action => params[:action], :type => '5', :class => "btn"  %>
</p>

I would like to use a button group, and have built this example using Twitter Bootstrap in plain HTML:

我想使用一个按钮组,并在纯 HTML 中使用 Twitter Bootstrap 构建了这个示例:

<div class="btn-group">
  <button class="btn">Sentence</button>
  <button class="btn">Question</button>
  <button class="btn">Mnemonic</button>
  <button class="btn">Article</button>
  <button class="btn">Recommendation</button>
</div>

I cannot get the links converted to the button row. I have tried various styling options as well as attempting to use button_to. Ideally I would like to further style the buttons so that the currently selected one is a different color than the others.

我无法将链接转换为按钮行。我尝试了各种样式选项并尝试使用button_to。理想情况下,我想进一步设置按钮的样式,以便当前选择的按钮与其他按钮的颜色不同。

EDIT

编辑

Output of HTML as suggested:

按照建议输出 HTML:

<div class="btn-group">
  <a href="/dialog_catagories?class=btn&amp;type=1">Sentence</a>
  <a href="/dialog_catagories?class=btn&amp;type=2">Question</a>
  <a href="/dialog_catagories?class=btn&amp;type=3">Mnemonic</a>
  <a href="/dialog_catagories?class=btn&amp;type=4">Article</a>
  <a href="/dialog_catagories?class=btn&amp;type=5">Recommendation</a>
</div>

The btn-group is preventing the buttons from rendering on the page for some reason, even though it is in the html.

btn-group 由于某种原因阻止按钮在页面上呈现,即使它在 html 中。

EDIT 2

编辑 2

HTML code as per Varun's answer:

根据 Varun 的回答,HTML 代码:

<div class="btn-group">
  <a href="/dialog_catagories?html%5Bclass%5D=btn&amp;type=1">Sentence</a>
  <a href="/dialog_catagories?html%5Bclass%5D=btn&amp;type=2">Question</a>
  <a href="/dialog_catagories?html%5Bclass%5D=btn&amp;type=3">Mnemonic</a>
  <a href="/dialog_catagories?html%5Bclass%5D=btn&amp;type=4">Article</a>
  <a href="/dialog_catagories?html%5Bclass%5D=btn&amp;type=5">Recommendation</a>
</div>

SOLUTION

解决方案

@Varun's suggested answer worked in the end:

@Varun 的建议答案最终奏效了:

<div class="btn-group">
  <%= link_to "Sentence", "?type=1", :class => "btn" %>
  <%= link_to "Question", "?type=2", :class => "btn" %>
</div>

I am a bit unclear how the functionality is still working, as the following is not being explicitly passed:

我有点不清楚该功能是如何工作的,因为没有明确传递以下内容:

:controller => params[:controller], :action => params[:action]

But it is working fine. Thank you.

但它工作正常。谢谢你。

回答by vvohra87

Try this:

尝试这个:

<div class="btn-group">
  <%= link_to "Sentence", :controller => params[:controller], :action => params[:action], :type => '1', :html => { :class => "btn" } %>
  <%= link_to "Question", :controller => params[:controller], :action => params[:action], :type => '2', :html => { :class => "btn" } %>
  <%= link_to "Mnemonic", :controller => params[:controller], :action => params[:action], :type => '3', :html => { :class => "btn" } %>
  <%= link_to "Article", :controller => params[:controller], :action => params[:action], :type => '4', :html => { :class => "btn" }  %>
  <%= link_to "Recommendation", :controller => params[:controller], :action => params[:action], :type => '5', :html => { :class => "btn" } %>
</div>

Edit

编辑

Try this:

尝试这个:

<div class="btn-group">
  <%= link_to "Sentence", "?type=1", :class => "btn" %>
  <%= link_to "Question", "?type=2", :class => "btn" %>
</div>

回答by user3337734

I'm still on Rails 2 and the html=> { :class => :btn } wasn't working for me.

我仍在使用 Rails 2,而 html=> { :class => :btn } 对我不起作用。

I did this and it renders/works nicely

我这样做了,它渲染/工作得很好

        <div class="btn-group">
          <%= link_to "Pool my Order", 
             {:controller => :purchasing, :action => :search_for_salesdoc,
              :sold_to_number => sold_to, doc_number => @cart.doc_number, 
              :search_option => :pool},
             :class => "btn btn-success align-center" %>
        </div>

回答by Tigraine

Whats wrong with your first code but converting the p to a ? Bootstrap links can be styled like buttons with class 'btn'

你的第一个代码有什么问题,但将 p 转换为 a ?Bootstrap 链接的样式可以像带有“btn”类的按钮一样

<div class="btn-group">
  <%= link_to "Sentence", :controller => params[:controller], :action => params[:action], :type => '1', :class => "btn" %>
  <%= link_to "Question", :controller => params[:controller], :action => params[:action], :type => '2', :class => "btn" %>
  <%= link_to "Mnemonic", :controller => params[:controller], :action => params[:action], :type => '3', :class => "btn"  %>
  <%= link_to "Article", :controller => params[:controller], :action => params[:action], :type => '4', :class => "btn"  %>
  <%= link_to "Recommendation", :controller => params[:controller], :action => params[:action], :type => '5', :class => "btn"  %>
</div>

This should world.. Doing the select detection is also not that hard, just add a check into the link_to:

这应该是世界......做选择检测也不是那么难,只需在link_to中添加一个检查:

  <%= link_to "Recommendation", :controller => params[:controller], :action => params[:action], :type => '5', :class => selected? ? "btn btn-primary" : "btn"  %>

you still have to to somehow implement the selected? helper but I guess you can figure this out.

你仍然必须以某种方式实施选定的?帮手,但我想你可以解决这个问题。

Update: It seems your link_to helper is the problem because you use the controller, action in there. I think the right way for your link_to methods is:

更新:似乎您的 link_to 助手是问题所在,因为您在那里使用了控制器和动作。我认为您的 link_to 方法的正确方法是:

link_to "Sentence", :controller => :foo, :action => :bar, :html => { :class => :btn }