简体   繁体   中英

Ruby on Rails - drop down with add new element

Standard dropdown:

<%= f.select :category_cont, @categories, :include_blank => true %>

Now, I want to be able not only to pick categories from dropdown menu but also add new category on the fly. Of course I can have <%= f.text_area :category %> next to my dropdown menu but it does not seem to be very elegant. Is there any way to be able to chose values from the pull down menu but also to add new if required?

Check this Selectize.js

Search for "Single Item Select"

Check here when you choose option not exist it suggest to add it to options

如果您选择的选项不存在,请在此处检查,建议将其添加到选项中

app/javascript/application.js:

//= require_tree .
//= require selectize

$(function() {
  $('#category-select').selectize({
    create: true,
    sortField: 'text'
  });
});

application.css:

*= require selectize
*= require selectize.default

my form (view):

<%= f.select( 
        :category, 
        @categories, 
        {}, 
        {id: 'category-select' }
    ) %>

did the trick.

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