将模型内容加载到导航栏下拉列表中

时间:2016-05-27 03:25:08

标签: ruby-on-rails twitter-bootstrap-3

导航栏下拉列表的标准代码就像这样

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
          </ul>

如何使用db模型的内容填充dropdown-menu?我最近为这个<%= fea.select(:artist_id, options_from_collection_for_select(Artist.all, "id", "name")) %>的同一个模型的Rails表单的下拉字段做了这个。 Bootstrap导航栏有什么解决方案?

1 个答案:

答案 0 :(得分:3)

您必须逐一获取记录并在li中显示它们。

action_controller.rb

before_action :set_artists

private
  def set_artists
    @artists = Artist.all
  end

查看

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
  Dropdown<span class="caret"></span>
</a>
<ul class="dropdown-menu">
  <% @artists.each do |artist| %>
    <li><%= link_to artist.name, artist %></li>
  <% end %>
</ul>