1

When I am saving the applicant model it is not saving the nested model data to data base. I have pasted my controller code, model code & passing parameters

My parameters are :

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"=>""}

My controller code is:

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

My applicant model is

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

My work model is

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
7
  • in your controller user "save!" instead of "save" and post the raised error Commented Apr 22, 2013 at 4:39
  • also you need to add to attr_accessable in applicant model :works_attributes ,:educations_Attributes Commented Apr 22, 2013 at 4:41
  • tried both of this, its not working Commented Apr 22, 2013 at 4:56
  • i already have made :works_attributes ,:educations_Attributes accessible Commented Apr 22, 2013 at 4:58
  • make sure the content field in both are not nil because you reject saving work and education if the content field in both Commented Apr 22, 2013 at 6:42

2 Answers 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"=>""}

Sign up to request clarification or add additional context in comments.

2 Comments

its helps me a lot. Really thanks for it. but i want to know that why should it necessary to have one field named content in it?
reject_if means that, you should reject the save if the condition that you give evaluates to false. Since your condition checks { |a| a[:content].blank? }, you would need :content params.
1

Remember to two attr_accessible if child table data is not gettting saved

attr_accessible :first_name attr_accessible :works_attributes, :allow_destroy => true accepts_nested_attributes_for :works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.