Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - Invalid Date [closed]

I created a method that calculates a user's age based on their birthday.

I receive an invalid date message when running this in Rails Server.

Error Message

ArgumentError in Users#show
Extracted source (around line #23):
20 </li>
21 
22 <li>
23   <strong>Age:</strong> <%= "#{user_age}" %>
24 </li>
25
26 <li>

Method

def user_age
  Time.now.year - (@user.birthday.split('-').rotate(-1).join('-').to_date.year)
end

In the above method, I converted the user's birthday attribute from a string to a date.

I tested this in Rails Console, and it worked perfectly, but it doesn't work in Rails Server.

Edit Page

<% @title = "Edit Profile" %>

<h2>Update your information here</h2>

  <div class = "center">

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

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

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

    <% end %>

</div>

Users Controller (Important Actions)

class UsersController < ApplicationController

  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

  private

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

end

User.rb (important)

class User < ActiveRecord::Base

  VALID_DATE_REGEX = /\A(([1-9]|1[012])[-\/.]([1-9]|[12][0-9]|3[01])[-\/.](19|20)\d\d)|((1[012]|0[1-9])(3[01]|2\d|1\d|0[1-9])(19|20)\d\d)|((1[012]|0[1-9])[-\/](3[01]|2\d|1\d|0[1-9])[-\/](19|20)\d\d)\z/

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

  private

  def date_conversion
    self.birthday = self.birthday.gsub('/', '-')
  end

end

My user_age method works when I run it in Rails Console, but I receive an invalid date message when I load the app.

One possible problem is my latest migrations file, where I set :age to int.

Migrations File

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

I still don't understand why I receive the invalid date message. In my method, I clearly converted the :birthday field which was a string into a date.

That's why it worked in Rails Console. I don't know why it doesn't work in Rails Server.

Any ideas? Help is greatly appreciated.

Full Stack Trace (First couple lines)

activemodel (4.0.4) lib/active_model/attribute_methods.rb:439:in `method_missing'
activerecord (4.0.4) lib/active_record/attribute_methods.rb:167:in `method_missing'
app/views/users/show.html.erb:23:in `_app_views_users_show_html_erb___1002307970_28886880'
actionpack (4.0.4) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.4) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.4) lib/action_view/template.rb:141:in `render'
actionpack (4.0.4) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
actionpack (4.0.4) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
like image 840
Darkmouse Avatar asked Feb 06 '26 17:02

Darkmouse


2 Answers

It turns out this part was the culprit

def user_age
  Time.now.year - (@user.birthday.split('-').rotate(-1).join('-').to_date.year)
end

The birthday was recorded as

12/13/1991

I was splitting the string into arrays at the ('-') character, which didn't exist in the original string. That is why the date was invalid.

Changing the birthday to

12-13-1991

solved the problem.

like image 182
Darkmouse Avatar answered Feb 09 '26 06:02

Darkmouse


You've missed >:

23    <strong>Age:</strong> <%= "#{user_age}" %>
like image 40
Andrey Deineko Avatar answered Feb 09 '26 07:02

Andrey Deineko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!