簡體   English   中英

未在Rails多選表單中設置默認值

[英]Defaults not set in Rails multiple-select form

在我的應用程序中,一篇文章可以有多個標簽。 創建文章時,我可以使用多選下拉列表和Rails Select2 gem將多個標簽添加到文章中。

問題是,當我編輯文章時,沒有選擇任何標簽,並且用戶必須再次選擇它們。

<%= form_for(@article) do |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>

有什么建議么?

當我將其放在頁面底部時,該方法就可以工作了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM