When a user saves a form, I want to do the following check before saving
if header2 is null
header2 = header1
I don't want to add validation and stop the save action, because header2 is only needed in rare occasions. I'm not sure how to have the update method in the controller do this. The command to commit the update is
if @entry.update_attributes(params[:entry])
So should I be modifying the params[:entry][:header2] field? I tried to do this with the following code but it isn't updating
params[:entry][:header2] = params[:entry][:header1]
Any help would be appreciated. I'm starting to feel I should handle this on the client side with javascript instead.
--EDIT: Added to original Post Below--
I have tried coderama's suggestions below and neither work for me
validate :data_present
def data_present
self.header2 = self.header1 if self.header2 == nil
end
and
def update
@entry = Entry.find(params[:id])
params[:entry][:header2] = params[:entry][:header1] unless params[:entry][:header2].present?
respond_to do |format|
if @entry.update_attributes(params[:entry])....
SQL
mysql> select header1, header2 from entries where title = "new";
+---------+---------+
| header1 | header2 |
+---------+---------+
| Blah | NULL |
+---------+---------+
1 row in set (0.00 sec)
I want be able to open in edit mode, change nothing (field is already null) and hit save and have this code changed. Have also tried for making new entry records and it doesn't work there either.