1

update下列行動nested_form的作品,但在create我得到這個錯誤:如何在新操作中使用與belongs_to關聯的嵌套屬性?

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

回答

1

您應該更改您的route.rb了。

resources :parent do 
    resources :child 
end 

查看Jose Valim的有用實施 - inherited-resources

+0

我可以在你的控制器中看到,有缺失的部分,建立資源之間的聯繫。你可以在這裏參考[this discusion]:http://stackoverflow.com/questions/5207038/the-better-way-to-pass-the-foreign-key-value-to-the-rails-controller。 –

相關問題