Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:08:11+00:00 2026-05-17T00:08:11+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:08:12+00:00Added an answer on May 17, 2026 at 12:08 am

    Here you go, along with the specs to prove

    /app/models/model_name.rb

    class ModelName < 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?
      end
    
    end
    

    And the spec: /spec/models/model_name_spec.rb

    require 'spec_helper'
    
    describe ModelName do
    
      before(:each) do
        @model_name = ModelName.new
        @header_one_text = "HeaderOneText"
        @header_two_text = "HeaderTwoText"
      end
      it "should should set header 2 if header 1 is blank" do
        @model_name.header_2 = @header_two_text
        @model_name.valid?
        @model_name.header_1.should == @header_two_text
      end
    
      it "should leave header 1 if both are set" do
        @model_name.header_1 = @header_one_text
        @model_name.header_2 = @header_two_text
        @model_name.valid?
        @model_name.header_1.should == @header_one_text
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to produce following behaviour: User in the browser saves some entity. After
i have the following schema and i want to create a Form with Symfony
So I have a form that I want the user to use to update
I have a user form for text phrases and I want to save the
I have following form. <form action= method=post id=save-user> <button type=button name=save-user>Save</button> <button type=button name=save-user-close>Save
I'm checking my form with RSV validator. Want to get work following: Let's say
I'm trying to update only some information for a user. Specifically, I don't want
In the dialog form below, I have 2 buttons: Save & Cancel. When user
Goal : After user saves data to my mysql DB, a JSON teaser of
I'm writing a Word/VBA macro for a document template. Every time a user saves/creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.