我的@minisets
新表单中有一个多选(使用bootstrap-multiselect),旨在通过@scales
表将@miniset
与@sizes
相关联。
协会工作正常。我坚持的是如何循环遍历多个:scale_id提交来自create
表中的多选和@sizes
行所有。
关注this answer我一直在尝试使用split然后循环创建,但我认为这个答案与HABTM关系有关,而我的是has_many_through意味着我需要一个不同的解决方案?
在我的minisets控制器中我有
def new
@miniset = Miniset.new
@miniset.sizes.build
end
def create
@miniset = Miniset.new(miniset_params)
if @miniset.save
params[:scale_id].split(',').each do |id|
@miniset.sizes.create(params[:sizes_attributes])
end
redirect_to @miniset
else
render 'new'
end
end
private
def miniset_params
params.require(:miniset).permit(:name, :release_date, :material, :pcode, :notes, :quantity, :random, productions_attributes: [:id, :manufacturer_id, :miniset_id], sizes_attributes: [:id, :scale_id, :miniset_id], sculptings_attributes: [:id, :sculptor_id, :miniset_id])
end
end
在我看来,我有
<%= f.fields_for :sizes do |size_fields| %>
<%= size_fields.label :scale_id, simple_pluralize(@miniset.scales.count, 'Scale') %>
<%= size_fields.select :scale_id,
options_from_collection_for_select(Scale.all, :id, :name, @miniset.scales.map(&:id)),
{},
{class: 'multiselect', multiple: true} %>
<% end %>
<script type="text/javascript">
$(document).ready(function() {
$('.multiselect').multiselect();
});
</script>
当我提交时,我正在收到nil:NilClass`错误undefined method
split'。
我认为这可能是因为日志显示在两个填充之前传递的空scale_id并且split不接受nil?提交两个比例时,这是日志。
Started POST "/minisets" for 127.0.0.1 at 2014-01-30 10:49:59 +0000
Processing by MinisetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R0RxDMwB5/ytSb5qgjIlVR5as0/DTkstgFMDXcefDnc=", "miniset"=>{"name"=>"Test for size", "quantity"=>"10", "random"=>"0", "material"=>"Hard Plastic", "sizes_attributes"=>{"0"=>{"scale_id"=>["", "1", "5"]}}, "pcode"=>"", "release_date(1i)"=>"", "release_date(2i)"=>"", "release_date(3i)"=>"", "notes"=>""}, "Set Scale"=>{"#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Size:0x007fcf643c29f0>"=>""}, "commit"=>"Add set"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'd59f28d384d62b71719dd845b4e5353cdd993016' LIMIT 1
Unpermitted parameters: scale_id
SQL (0.9ms) INSERT INTO "minisets" ("created_at", "material", "name", "notes", "pcode", "quantity", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00], ["material", "Hard Plastic"], ["name", "Test For Size"], ["notes", ""], ["pcode", ""], ["quantity", 10], ["updated_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00]]
SQL (0.6ms) INSERT INTO "sizes" ("created_at", "miniset_id", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00], ["miniset_id", 41], ["updated_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00]]
(4.2ms) commit transaction
Completed 500 Internal Server Error in 153ms
NoMethodError (undefined method `split' for nil:NilClass):
app/controllers/minisets_controller.rb:19:in `create'
我确定分割后的内容不正确,但在分割工作之前我无法使用它。我可以通过在分割之前添加to_s
来消除错误,但我没有得到更好的结果。
现在这个多选的进展非常缓慢,所以任何帮助都非常赞赏。
答案 0 :(得分:1)
感谢this精彩的youtube视频,我解决了我的问题。
我的表格:
<%= f.fields_for(@size) do |sf| %>
<%= sf.label simple_pluralize(@miniset.scales.count, 'Scale') %>
<%= collection_select( :scales, :id, @all_scales, :id, :name,
{},
{class: 'multiselect', multiple: true}) %>
<% end %>
在我的minisets_controller中,我有以下新的和创建动作:
def new
@miniset = Miniset.new
@all_scales = Scale.all
@size = @miniset.sizes.build
end
def create
@miniset = Miniset.new(miniset_params)
params[:scales][:id].each do |scale|
if !scale.empty?
@miniset.sizes.build(:scale_id => scale)
end
end
if @miniset.save
redirect_to @miniset
else
render 'new'
end
end
完美无缺。如果其他人遇到同样的问题,尝试通过has_many_through让多选用于rails,我建议观看该视频。很高兴。