简体   繁体   English

Rails 应用程序不保存嵌套表单模型数据

[英]Rails app not saving nested form model data

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

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.您指定的 reject_if 表示当参数中没有 :content 时,它不应保存子模型。 So make sure that your params has content attribute with a value in it.因此,请确保您的 params 具有带有值的内容属性。 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"=>""} {"utf8"=>"✓", "authenticity_token"=>"5F8h7qG2pez9Rsutnq3JXYyXbPkbVJAlgfIfsE1bdUw=", "申请人"=> {"job_id"=>"1", "first_name"=>"sanyam"=>"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"=>"" }

Remember to two attr_accessible if child table data is not gettting saved如果没有保存子表数据,请记住两个 attr_accessible

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM