我正在使用Simple_form / Rails 4并希望在同一表单上创建多个关系。我的模特是:
class Characteristic < ActiveRecord::Base
has_and_belongs_to_many :recipes
end
和
class Recipe < ActiveRecord::Base
has_and_belongs_to_many :characteristics
end
我有一个联接表
create_table "characteristics_recipes", force: true do |t|
t.integer "recipe_id"
t.integer "characteristic_id"
end
我的表单看起来像
<%= simple_form_for(@recipe, html: {class: 'form-horizontal' }) do |f| %>
<div class="form-group">
<%= f.association :characteristics,
:as => :collection_select,
collection: Characteristic.where(category: "Prep Time"),
prompt: "Choose a Prep Time", label: "Prep Time",
value_method: :id %>
</div>
<div class="form-group">
<%= f.association :characteristics,
:as => :collection_select,
collection: Characteristic.where(category: "Cook Time"),
prompt: "Choose a Cook Time", label: "Cook Time",
value_method: :id %>
</div>
<%= f.button :submit %>
<% end %>
我的参数只捕获第二个关联,它是一个字符串而不是一个int ...
{"utf8"=>"✓",
"authenticity_token"=>"GR2mvo1s7=",
"recipe"=>{ "characteristic_ids"=>"7"},
"commit"=>"Create Recipe",
"action"=>"create",
"controller"=>"recipes"}
在我的控制器中,我有以下作为我允许的参数:
def recipe_params
params.require(:recipe).permit(:characteristic_ids)
end
我已经尝试将其更改为但是这并不重要,因为我没有捕获第一个关联
def recipe_params
params.require(:recipe).permit(:characteristic_ids => [])
end
如何让多个协会在这里工作?
谢谢!!!!