如何在表单重新显示时选择一个集合来显示所选的已提交值?例如,如果其他字段未验证,则多选框应显示所选项目
<%= collection_select(:coupon, :activity_ids,
current_user.provider.activities, :id, :name,
{},
{:multiple => true, :class => 'chosens-select', :name=>'coupon[activity_ids][]'}) %>
答案 0 :(得分:1)
这样的事情会发生。
<%= collection_select(:coupon, :activity_ids,current_user.provider.activities,
:id, :name, {:selected => @coupon.activity_ids}, {:multiple => true, :class => 'chosens-select', :name=>'coupon[activity_ids][]'}) %>
答案 1 :(得分:1)
我建议用户SimpleForm。你会做的:
simple_form_for(my_model) do |f|
f.association :activities
f.button :submit
end
它将简化您的所有表格。
答案 2 :(得分:0)
感谢那些发布答案的人,它促使我放弃了#34;修复&#34;表格再看看我的控制器。事实证明我在params声明中犯了一个错误:
def coupon_params
params.require(:coupon).permit(:id, :provider_id, :code, :max_uses, :max_uses_per_customer, :name, :expires_at,
:dollar_discount, :percent_discount, :starts_at, :activity_ids => {})
end
应该在哪里
def coupon_params
params.require(:coupon).permit(:id, :provider_id, :code, :max_uses, :max_uses_per_customer, :name, :expires_at,
:dollar_discount, :percent_discount, :starts_at, :activity_ids => [])
end
发现差异? activity_ids应该是一个数组,而不是哈希
:activity_ids => []