我已经使用Cocoon添加了嵌套表单,但是没有发送属性而且没有保存任何内容。到目前为止,我尝试过复数/单数但没有运气。到目前为止我能得到的最好的是它在我以单数制作f.simple_fields_for时发送参数但是后来仍然只发送了1个(只有第一个添加的)并且我得到了未经许可的参数vacancyschedule',但据我所知,我遵循明确命名的文档。 任何帮助表示赞赏。
我的设置:
模型
vacancy.rb
has_many :vacancyschedules, dependent: :destroy
accepts_nested_attributes_for :vacancyschedules, reject_if: :all_blank, allow_destroy: true
vacancyschedule.rb
belongs_to :vacancy
控制器
def vacancy_params
params.require(:vacancy).permit(:address, :name, :street, :city, :zip, :country, :description, :jobtype_id, :recruiter_id, :start_date, :end_date, :start_hour, :end_hour, :skill_id, :wage_cents, :tag_list, :jobterm_id, vacancyschedule_attributes: [:start_date, :end_date, :start_hour, :end_hour, :address, :_destroy, :id])
end
常规表单
= simple_form_for @vacancy do |f|
... regular fields that are saving correctly ...
.vacancyschedules
= f.simple_fields_for :vacancyschedules do |vacancyschedule|
=render "vacancyschedule_fields", :f, :vacancyschedule
.links
=link_to_add_association 'add schedule', f, vacancyschedules
部分
.nested-fields
.field
=f.input :start_hour, as: :time
.field
=f.input :end_hour, as: :time
=link_to_remove_association "", f
编辑如上所述,
当我执行simple_fields_for:vacancyschedule时,我得到了一些参数(尽管它只是第一个参数,如果添加了更多的表格则会忽略)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iF/6usHuIszbWH+JCERMZHKPGQu59VrLePlyR1Xea8hqlTbGdgCRJmG9bwh
XlCczK7xLK3JW96vkngwY5PzYJg==", "vacancy"=>{"recruiter_id"=>"2", "name"=>"", "address"=>"", "description"=>"", "jobtype_id"=>"", "jobterm_id"=>"",
"wage_cents"=>"", "vacancyschedule"=>{"start_hour(1i)"=>"2015", "start_hour(2i)"=>"12", "start_hour(3i)"=>"9", "start_hour(4i)"=>"09",
"start_hour(5i)"=>"00", "end_hour(1i)"=>"2015", "end_hour(2i)"=>"12", "end_hour(3i)"=>"9", "end_hour(4i)"=>"17", "end_hour(5i)"=>"00",
"start_date"=>"1/1/1"}}, "commit"=>"Create Vacancy"}
Unpermitted parameter: vacancyschedule
当复数时,没有发送任何内容..
更新2
在我的vacancycontroller中新建和创建方法
def new
@vacancy = Vacancy.new
# @vacancyschedules = @vacancy.vacancyschedules
end
def create
@vacancy = Vacancy.create(vacancy_params)
# Vacancyschedule.create(vacancyschedule_params)
redirect_to show_created_vacancy_path(@vacancy)
end
据我所知,只有新的和创造的母亲"模型是必要的。我之前也尝试过在那里添加计划而没有成功进行测试..
答案 0 :(得分:1)
我想错误地定义渲染部分:
$productIds = [1 => [
'created_by' => 1,
'updated_by' => 1
],3 => [
'created_by' => 1,
'updated_by' => 1
],5 => [
'created_by' => 1,
'updated_by' => 1
],6 => [
'created_by' => 1,
'updated_by' => 1
]];
答案 1 :(得分:1)
#vacancy.rb:
has_many :vacancyschedules, dependent: :destroy
accepts_nested_attributes_for :vacancyschedules,
reject_if: :all_blank,
allow_destroy: true
和vacancyschedule.rb:
#vacancyschedule.rb:
belongs_to :vacancy
在空缺控制器中:
#vacancy_controller.rb:
def create
@vacancy = Vacancy.new(vacancy_params)
if @vacancy.save
redirect_to :back, notice: "vacancy successfully created."
else
render :new
end
end
private
def vacancy_params
params.require(:vacancy).permit(:address, :name, :street, :city..., vacancyschedules_attributes: [:start_date, :end_date, :start_hour...])
end
在您看来:
#_form.html.haml
= simple_form_for @vacancy do |f|
... regular fields that are saving correctly ...
.vacancyschedules
= f.simple_fields_for :vacancyschedules do |vacancyschedule|
=render "vacancyschedule_fields", f: vacancyschedule
.links
=link_to_add_association 'add schedule', f, vacancyschedules
#vacancyschedule_fields.html.haml
.nested-fields
.field
=f.input :start_hour, as: :time
.field
=f.input :end_hour, as: :time
=link_to_remove_association "X", f
这应该适合你。
答案 2 :(得分:1)
您的强参数定义(在此处发布)是错误的:您编写vacancyschedule_attributes
(单数)并且必须匹配关联,因此它应该是vacancyschedules_attributes
。
完全不相关的旁注:我更喜欢类名VacancySchedule
,因此更喜欢关联名vacancy_schedules
。这更容易阅读。
答案 3 :(得分:0)
首先,谢谢大家的帮助。
我发现代码本身并没有错。 出于某种原因,周围的div正在搞乱cocoon提供的javascript。 我剥离了所有的div /样式,它正确地提交和保存。 对我来说,下一个挑战是逐步添加样式并查看问题发生的位置。