如何在新动作中将嵌套属性与belongs_to关联一起使用? [英] How to use nested attributes with belongs_to association on new action?
本文介绍了如何在新动作中将嵌套属性与belongs_to关联一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在update
动作上,以下nested_form
起作用,但是在create
上,我收到此错误:
On the update
action the following nested_form
works, but on create
I get this error:
Couldn't find Student with ID=12 for Cv with ID=
控制器:
def new
@cv = Cv.new
@cv.student = current_student
end
def create
@cv = Cv.new(params[:cv])
if @cv.save
redirect_to student_dashboard_path,
notice: t('activerecord.successful.messages.created', model: @cv.class.model_name.human)
else
render 'new'
end
end
型号:
class Cv < ActiveRecord::Base
attr_accessible :student_attributes
belongs_to :student
accepts_nested_attributes_for :student
end
视图:
= f.simple_fields_for :student do |s|
= s.input :english_level, collection: [['Low', 1], ['High', 2]]
推荐答案
您也应该更改route.rb.
You should make changes to your route.rb too.
resources :parent do
resources :child
end
看看Jose Valim的这个有用的实现-继承的资源.
Take a look at this useful implementation by Jose Valim - inherited-resources .
这篇关于如何在新动作中将嵌套属性与belongs_to关联一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文