2015-06-08 59 views
0

你好我得到這個在我的PARAMS保存PARAMS在before_save回調值

參數:{ 「UTF8」=> 「✓」, 「authenticity_token」=>「YmYvS/vPVB9FD7 + XvDx0 + K8UKHReHLifTmc91xD8pASSNqg9d0b/77NnodxQYhbN806661eSBk9vQwTHib3w/w ==「,」employee「=> {」profile_attributes「=> {」name「=>」dinshaw「, 」date_of_joining「=>」2015年6月8日「,」date_of_birth「=> 2015年6月27日「, 」aniversary「=>」2015年6月3日「,」last_position「 「=>」BHJ47「},」email「=>」[email protected]「, 」role「=>」SUPER-ADMIN「,」d epartment_id「=>」1「,」designation_id「=>」1「, 」is_active「=>」1「,」contact_attributes「=> {」phone_no「=>」9856321470「, 」current_address1「=>」test1dinsh 「,」current_address2「=>」awwwwwwwww「, 」country「=>」IN「,」city「=>」Indore「,」zip「=>」452001「, 」emergency_contact_person「=>」abhi「, 「emergency_contact_no」=>「9632145870」,「relation」=>「farji」}, 「permanent_address_attributes」=> {「address1」=>「testbghghgjh」, 「address2」=>「hdvhdkhfdhnbdfds」,「country」=> 「IN」,「city」=>「Ghaziabad」, 「zip」=>「12356」}},「contact」=> {「state_code」=>「MP」}, 「permanent_address」=> {「state_code 「=>」CH「},」commit「=>」更新 員工「,」id「=>」1「}

現在在我的employee.rb我已經包含了這個代碼

class Employee < ActiveRecord::Base 
    belongs_to :department 
    belongs_to :designation 
    has_one :profile 
    has_one :contact 
    has_one :permanent_address 
    has_many :attachments, as: :attachable 
    accepts_nested_attributes_for :attachments 
    accepts_nested_attributes_for :profile, :contact, :permanent_address 
    before_save :set_state_code 

    def set_state_code 
    self.contact.state_code = 'AP' 
    self.permanent_address.state_code = 'MP' 
    end 

    def create_association_instance 
    self.build_profile unless self.profile 
    self.build_contact unless self.contact 
    self.build_permanent_address unless self.permanent_address 
    self.attachments.build unless self.attachments.present? 
    end 

end 

但我想

self.contact.state_code = params[:contact][:state_code] && 
self.permanent_address.state_code = params[:permanent_address][:state_code] 

我沒有得到如何將其在模型中獲取。請指導我。在此先感謝

回答

0
class Employee < ActiveRecord::Base 
    attr_accessor :contact_state_code, :address_state_code 
    before_save :set_state_code 

    def set_state_code 
    self.contact.update_column(state_code: self.contact_state_code) 
    self.permanent_address.update_column(state_code: self.address_state_code) 
    end 
end 

和控制器這樣做:

params[:employee][:contact_state_code] = params[:contact][:state_code] 
params[:employee][:address_state_code] = params[:permanent_address][:state_code] 
+0

不,我不想在控制器 – railslearner

+0

任何代碼。如果怎麼該值將通過保存你不會發送任何模型,然後模型..? –

相關問題