Search code examples
ruby-on-railsrubyregexnullupdating

Nil Values are being recorded when a form is submitted


My apologies if this has been asked before, but I couldn't find the answer on here.

I recently created a date_regex validation method for validating dates

VALID_DATE_REGEX = /\d{2}[\/]\d{2}[\/]\d{4}/

I know the regex isn't good right now, and I'll update that later, but I don't need any help with that.

I added a field that lets users select their birthday. I had to update my users model to allow such functionality.

Migrations File

class AddAgeToUsers < ActiveRecord::Migration
  def change
    add_column :users, :age, :integer
    add_column :users, :birthday, :date
    add_column :users, :location, :string
  end
end

Here is my validation in Users.rb

validates :birthday, format: { with: VALID_DATE_REGEX }, :on => :update

Trying to figure out what the problem was, I fired up Rails Console

u = User.find(1)
u.update_attributes(:birthday => '12/22/1992')
=> u.save (false)
u.errors.full_messages
=> Birthday is invalid
u.birthday
=> nil

It looks like the problem is I expected the value of birthday to be '12/22/1992' but instead it was nil.

I figured out that that was the reason why the record wasn't updated in the database. I don't know if it's a problem with the attribute being stored as a date, but it doesn't work.

Users Controller

def edit
  @user = User.find(params[:id])
end

def update
  @user = User.find(params[:id])

  if @user.update_attributes(user_params)
    redirect_to @user
    flash[:success] = "Your profile has been updated"
  else
    render 'edit'
  end
end

Users Edit Page

<% @title = "Edit Profile" %>

<h2>Update your information here</h2>

  <div class = "center">

    <%= form_for @user do |f| %>

    <p>
      <%= f.label :name, 'Username', class: 'marker' %>
      <%= f.text_field :name %>
    </p>

    <p> 
      <%= f.label :email, class: 'marker' %>
      <%= f.text_field :email %>
    </p>

    <p> 
      <%= f.label :birthday, class: 'marker' %>
      <%= f.text_field :birthday %>
    </p>

    <p> 
      <%= f.label :location, class: 'marker' %>
      <%= f.text_field :location %>
    </p>

    <p>
      <input class="btn btn-primary" type="submit" value="Update">
    </p>

    <% end %>

</div>

Help is appreciated.

Update

I changed the birthday attribute to string type. Now, when I run update_attributes(:birthday) in the console, it saves, but when I type the same thing in the text box it doesn't save.

I can't figure this out.


Solution

  • Alright guys, I finally figured it out. There were 2 problems, the first with my birthday attribute and the second with my users_params.

    As mentioned earlier, I declared my birthday attribute as a date field. It works better if the attribute is of string type.

    add_column :users, :birthday, :string
    

    I then migrated the file and worked with Rails Console.

    u = User.first
    u.update_attributes(:birthday => "12-22-1992")
    

    This time the value was recorded properly and saved.

    u.birthday
    => "12-22-1992"
    

    I then tested it out in my Rails app in my edit form.

    The value was still invalid and I just found out why.

    The problem was with my user_params method

    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation,
     :location)
    end
    

    I simply added the :birthday attribute to my users_params and it fixed my problem. This time the birthday was successfully saved.

    Thanks everyone for your help.