使用ruby代码输出在bootstrap按钮行中添加下拉列

时间:2016-04-28 05:23:50

标签: html ruby-on-rails ruby erb

在我的Rails应用程序上,我有一个按钮,我想将其中一个放入一个下拉菜单中,该菜单将显示从db表中提取的项目,ruby代码可以工作,但我可以& #39; t让它显示为下拉。

这是红宝石代码:

<% @all_makes.each do |allMakesDisplay| %>
  <a id="allMakesDisplay"><%= link_to allMakesDisplay.name, make_index_path(make_name: allMakesDisplay.name) %></a>
<% end %>

这是下拉行:

&#13;
&#13;
<div class="collapse navbar-collapse top-bar" id="bs-example-navbar-collapse-1">
  <div class="row">
    <div class="col-md-3">
      <button class="btn btn-lg btn-secondary btn-block">
        <a href="/">Home</a>
      </button>
    </div>
    <div class="col-md-3">
      <div class="dropdown">
        <button class="btn btn-lg btn-secondary btn-block dropdown-toggle" type="button" data-toggle="dropdown">
          <a href="/dealership/category/make">Makes</a>
          <span class="caret"></span>
        </button>
        <% @all_makes.each do |allMakesDisplay| %>
          <ul class="dropdown-menu">
            <li>
              <a href="<%= link_to allMakesDisplay.name, dealership_category_make_path(allMakesDisplay) %>"></a>
            </li>
          </ul>
          <% end %>
      </div>
    </div>
    <div class="col-md-3">
      <button class="btn btn-lg btn-secondary btn-block">
        <a href="blog.html">Models</a>
      </button>
    </div>
    <div class="col-md-3">
      <button class="btn btn-lg btn-secondary btn-block">
        <a href="contact.html">My Portfolio</a>
      </button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

MAKES应该是下拉列表,应该返回12个项目。

由于

1 个答案:

答案 0 :(得分:1)

有一些事情正在发生:

  • #each循环应位于<ul>内。
  • link_to会创建一个锚标记<a>,因此不应将其用作href属性。 link_to

&#13;
&#13;
<div class="collapse navbar-collapse top-bar" id="bs-example-navbar-collapse-1">
  <div class="row">
    <div class="col-md-3">
      <button class="btn btn-lg btn-secondary btn-block">
        <a href="/">Home</a>
      </button>
    </div>
    <div class="col-md-3">
      <div class="dropdown">
        <button class="btn btn-lg btn-secondary btn-block dropdown-toggle" type="button" data-toggle="dropdown">
          <a href="/dealership/category/make">Makes</a>
          <span class="caret"></span>
        </button>
        
          <ul class="dropdown-menu">
            <!--for each element, create a <li> (not <ul>)-->
            <% @all_makes.each do |allMakesDisplay| %>
            <li>
              <%= link_to allMakesDisplay.name, 
                          dealership_category_make_path(allMakesDisplay) %>
            </li>
           <% end %> <!-- end #each loop-->
          </ul>
          
      </div>
    </div>
    <div class="col-md-3">
      <button class="btn btn-lg btn-secondary btn-block">
        <a href="blog.html">Models</a>
      </button>
    </div>
    <div class="col-md-3">
      <button class="btn btn-lg btn-secondary btn-block">
        <a href="contact.html">My Portfolio</a>
      </button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;