6

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

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

我想使用一个按钮组,并使用纯 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>

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

编辑

按照建议输出 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>

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

编辑 2

根据 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>

解决方案

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

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

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

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

但它工作正常。谢谢你。

4

3 回答 3

22

尝试这个:

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

编辑

尝试这个:

<div class="btn-group">
  <%= link_to "Sentence", "?type=1", :class => "btn" %>
  <%= link_to "Question", "?type=2", :class => "btn" %>
</div>
于 2012-11-22T13:57:29.643 回答
4

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

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

        <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>
于 2014-05-13T18:15:55.363 回答
2

您的第一个代码有什么问题,但将 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>

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

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

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

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

link_to "Sentence", :controller => :foo, :action => :bar, :html => { :class => :btn }
于 2012-11-22T13:24:33.540 回答