简体   繁体   中英

How do I implement bootstrap dropdown in rails with categories

I'm wanting to implement Bootstrap's dropdowns in my rails project similar to the code below provided on Bootstrap's website:

<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
    <li><a href="#">Action</a></li>
    <li><a href="#">Another action</a></li>
    <li><a href="#">Something else here</a></li>
    <li role="separator" class="divider"></li>
    <li><a href="#">Separated link</a></li>
  </ul>
</div>

I currently have a section in the project where I can add new/edit categories for use in the main form. The code I currently have for an unformatted select/dropdown is:

<%= f.select :category_id, Category.all.map { |category| [ category.name, category.id ] }, class: "form-control" %>

Which doesn't seem to apply any bootstrap formatting, I've tried changing the code around a bit to be:

<%= f.select :category_id, options_for_select([Category.all.map { |category| [ category.name, category.id ] }]), {}, class: "form-control" %>

This gives the dropdown box a bootstrap style box, but the dropdowns under the default selection don't have any formatting similar to Bootstrap's example. One of my categories is just called Pages, but the text inside the selectbox in the code above just shows ["pages", 1] where 1 is the ID.

Any help with this would be greatly appreciated :)

我认为正确的语法如下

 = f.select :category_id, options_for_select(Category.collect {|c| [c.name, c.id]}), { :include_blank => "Please select category"}, {:class => "form-control"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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