ruby on rails - Controller edit method, modify object data before commit -


When a user saves a form, I want to check the following before saving

 < Code> if header 2 empty header 2 = header1  

I do not want to stop adding validation and preventing the action, because Header2 is only required in rare occasions I'm not sure that the controller How to update the method in

If @ entry.update_attributes (params [: entry])

then modify the parameter I Should: [: entry] [: header 2] field? I tried to do this with the following code, but it

  parameter [: entry] [: header2] = params [: entry] [: header1]  

Any help would be appreciated I think I should handle it with Javascript on client side instead.

- Edit: Added to the original post below -

I have tried the suggestions of the coder below and do not work for me

  Validate: data_present def data_presentation itself Header2 = self.header1 if self.header2 == zero end  

and

  def update @entry = Entry.find (params [: id]) Params [: entry] [: header2] = params [: entry] [: header 1] till paramon [: entry] [: header 2] .present? Response_to do | Format | If the @ entry.update_attributes (parameter [: entry]) ....  

SQL

  mysql> Select Top 1, from the top 2 entries where the title = "new"; + --------- + --------- + | Top 1 | Header 2 | + --------- + --------- + | Blah | Faucet + --------- + --------- + 1 line set (0.00 seconds)  

I want to be able to open in editing mode, Do not change anything (the field is already null) and save the hit and this code has changed. They also tried to create new entry records and it does not work either there.

/app/models/model_name.rb

  Class ModelName & lt; ActiveRecord :: Base attr_accessor: header_1,: header_2 before_validation: header_check def header_check Return if self.header_2.blank? Self.header_1 = self.header_2 if self.header_1.blank? And speed  

and spec: /spec/models/model_name_spec.rb

  Describe the model name 'spec_helper' first Should do this (: each) do @model_name = ModelName.new@header_one_text = "HeaderOneText" @header_two_text = "HeaderTwoText" "Header 1 is empty, if" Header1 is empty "," do @ model_name.header_2 = @ Header_two_text @ model_name.valid "should be set to @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ Model_name.valid? @ Model_ Name.adorder_1.Sold == @ header_on_text and end  

Comments