如何使用嵌套的属性与新的动作belongs_to的关联?(How to use nested att

2019-10-17 09:27发布

update动作以下nested_form的作品,但在create我得到这个错误:

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]]

Answer 1:

您应该更改您的route.rb了。

resources :parent do
  resources :child
end

看看由何塞·Valim这个有用的实现- 遗传资源 。



文章来源: How to use nested attributes with belongs_to association on new action?