【问题标题】:How to use nested attributes with belongs_to association on new action?如何在新操作中使用带有 belongs_to 关联的嵌套属性?
【发布时间】:2012-11-17 14:26:23
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 nested-forms nested-attributes


    【解决方案1】:

    您也应该对您的 route.rb 进行更改。

    resources :parent do
      resources :child
    end
    

    看看 Jose Valim 的这个有用的实现 - inherited-resources

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多