Cla*_*abe 3 ruby-on-rails ruby-on-rails-3 select2-rails
我的问题是,我有,例如,Product,Category和ProductCategory.ProductCategory使产品具有多个类别
我想使用select2-rails gem(https://github.com/argerim/select2-rails)使用Select2(http://ivaynberg.github.io/select2/)实现这一点.
我已经知道如何关联模型,但我无法弄清楚如何实现Select2特定代码.
编辑:现在我看到我的问题不是关于select2,所以我添加了这个评论并改变了标题,希望它可以帮助别人
Cla*_*abe 10
现在我看到我的问题不是关于select2而是做多选.
使其工作的_form.html.erb中的代码是这样的:
<%= f.label :category_id %>
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {:selected => @product.category_ids, :include_blank => true}, {:class => 'col-xs-12 col-md-7 padding_15', :multiple => true} %>
Run Code Online (Sandbox Code Playgroud)
我还包括:category_ids在attr_accessiblemodels/product.rb上
而select2具体,我包含在.js文件中
$(document).ready(function() {
$('#product_category_ids').select2();
});
Run Code Online (Sandbox Code Playgroud)
我包括这些链接,因为他们帮助我,但注意差异取决于Ruby/Rails版本
只是为了让你知道,如果这个collection_select是我表单中的最后一行,一些表单字段被禁用,尽管在源代码中没有任何说明.更改订单此问题不存在.
我也不知道为什么外观与其他领域有点不同(我使用的是Bootstrap 3)
| 归档时间: |
|
| 查看次数: |
3417 次 |
| 最近记录: |