1

申請者モデルを保存するときに、ネストされたモデル データをデータベースに保存していません。コントローラーコード、モデルコード、およびパラメーターの受け渡しを貼り付けました

私のパラメータは次のとおりです。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"5F8h7qG2pez9Rsutnq3JXYyXbPkbVJAlgfIfsE1bdUw=", "applicant"=>
{"job_id"=>"1", "first_name"=>"sanyam", "location"=>"", "email"=>"", "mob_no"=>"", "alternative_no"=>"", "last_name"=>"jain", "works_attributes"=>{"1366604495995"=>
{"title"=>"M Tech", "company_name"=>"", "start_month"=>"", "start_year"=>"", "end_month"=>"", "end_year"=>"", "description"=>"", "_destroy"=>"false"}, "1366604506595"=>
{"title"=>"B Tech", "company_name"=>"", "start_month"=>"", "start_year"=>"", "end_month"=>"", "end_year"=>"", "description"=>"", "_destroy"=>"false"}}, "linkedin"=>"", 
"twitter"=>"", "facebook"=>"", "message"=>""}, "submit"=>""}

私のコントローラーコードは次のとおりです。

def createProfile
@applicant = Applicant.new(params[:applicant])
@applicant.save
end

私の応募者モデルは

class Applicant < ActiveRecord::Base
attr_accessible :first_name, :last_name, :location, :email, :mob_no, :alternative_no, :linkedin, :facebook, :twitter, :message, :resume, :job_id
has_many :works, :dependent => :destroy
has_many :educations, :dependent => :destroy
attr_accessible :works_attributes, :educations_attributes
accepts_nested_attributes_for :works, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

accepts_nested_attributes_for :educations, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

私のワークモデルは

class Work < ActiveRecord::Base
attr_accessible :applicant_id, :company_name, :description, :end_month, :end_year, :start_month, :start_year, :title
belongs_to :applicant
validates_associated :applicant
end
4

2 に答える 2

2

The reject_if that you have specified says that it should not save the child model when the params does not have :content in it. So make sure that your params has content attribute with a value in it. Ur params should look sort of like this:

{"utf8"=>"✓", "authenticity_token"=>"5F8h7qG2pez9Rsutnq3JXYyXbPkbVJAlgfIfsE1bdUw=", "applicant"=> {"job_id"=>"1", "first_name"=>"sanyam", "location"=>"", "email"=>"", "mob_no"=>"", "alternative_no"=>"", "last_name"=>"jain", "works_attributes"=>{"1366604495995"=> {"title"=>"M Tech", "company_name"=>"", "start_month"=>"", "start_year"=>"", "end_month"=>"", "end_year"=>"", "description"=>"", "content"=>"some content", "_destroy"=>"false"}, "1366604506595"=> {"title"=>"B Tech", "company_name"=>"", "start_month"=>"", "start_year"=>"", "end_month"=>"", "end_year"=>"", "description"=>"", "content"=>"some content", "_destroy"=>"false"}}, "linkedin"=>"", "twitter"=>"", "facebook"=>"", "message"=>""}, "submit"=>""}

于 2013-04-22T09:29:19.390 に答える