jquery select2 - Defaults not set in Rails multiple-select form -


in application, 1 article can have multiple tags. when creating article, able add multiple tags article using multiple-select dropdown , rails select2 gem.

the problem when edit article, none of tags selected, , user has select them on again.

<%= form_for(@article) |f| %>   <%= f.select :tags, options_for_select(@tags.collect {|t| [ t.id, t.title] }),{:selected=>@article.tags}, :multiple => true, :id => "article_tags", :class => "form-control" %><br>   <%= f.submit 'update', :class => "btn" %><br> <% end %>  <script>     $(document).ready(function() {       $("#article_tags").select2({multiple:true});     }); </script> 

any suggestions?

this ended working when put @ bottom of page:

<script>   $(document).ready(function() {     $("#tags").select2({multiple:true});     $('select').select2().select2('val', 'tag one, tag two, tag three');     $('select').select2();   }); </script> 

Comments