javascript - Rails select with suggestions and remote data -


i have, example, project , category models:

# project.rb has_and_belongs_to_many :categories  # category.rb has_and_belongs_to_many :projects 

in front-end need to:

  • show input name of project category
  • suggest / autocomplete category name if exist
  • if doesn't exists, need dynamically create , paste name input , hold id, send id server when user submits form.

so, have categories controller, have methods search , create. search returns in json format pairs of id , name of suggested categories; create saves category database , returns pair of id , name when user submits new category input. multiple category input.

also, in other part of website meet models, uses category model not has_and_belongs_to, belongs_to. there need show select box single value of category, same logic multiple input.

select2 or selectize libs covers part of issue, have no idea how hold ids instead of names input , how render name of category input , send id when user submits form.

are there libs or technics solve issue?

i recommend watching ryan bates' excellent railscasts episode on this. demonstrates find_or_create type of functionality you're describing, using jquery tokeninput library.

if you're committed using select2, can use similar approach ryan's on back-end, while using tagging support in select2.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -