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 7996895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:49:29+00:00 2026-06-04T14:49:29+00:00

I’m starting with ruby on rails. I have a simple scaffold. Here is my

  • 0

I’m starting with ruby on rails. I have a simple scaffold.

Here is my model:

class Pet < ActiveRecord::Base
  belongs_to :user
  belongs_to :petRace
  attr_accessible :birth, :exactAge, :nick
  def initialize
    birth = DateTime.now.in_time_zone.midnight
  end
end

the html code

<%= form_for @pet, :html => { :class => 'form-horizontal' } do |f| %>
  <div class="control-group">
    <%= f.label :nick, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :nick, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :birth, :class => 'control-label' %>
    <div class="controls">
    <div class="input-append date datepicker" data-date="<%=@pet.birth.strftime("%d/%m/%Y") %>" data-date-format="dd/mm/yyyy">
        <%= f.text_field :birth, :class => 'input-append', :value => @pet.birth.strftime("%d/%m/%Y") %>
    <span class="add-on"><i class="icon-th"></i></span>
    </div>
  </div>

  <div class="form-actions">
    <%= f.submit nil, :class => 'btn btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                pets_path, :class => 'btn' %>
  </div>
<% end %>

the controller:

 def new
    @pet = Pet.new   
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @pet }
    end
  end

I just replace the original code for the :birth attribute, as you can see here:

<%= f.text_field :birth, :class => 'input-append', :value => @pet.birth.strftime("%d/%m/%Y") %>

when i select the option new the birth property seems to be no value and i get this execption

undefined method `[]' for nil:NilClass


Extracted source (around line #11):

8:      
9:  </script>
10: <%end%>
11: <%= form_for @pet, :html => { :class => 'form-horizontal' } do |f| %>
12:   <div class="control-group">
13:     <%= f.label :nick, :class => 'control-label' %>
14:     <div class="controls">

 app/views/pets/_form.html.erb:11:in `_app_views_pets__form_html_erb__3291519358612565784_70159905628180'
app/views/pets/new.html.erb:4:in `_app_views_pets_new_html_erb__1494749415896629355_70159907398120'
app/controllers/pets_controller.rb:28:in `new'

It’s my understanding that the birth value is set with the actual date and time(in the initialize method). Am i wrong or missing something? When i edit a record i have no problem.

Thanks in advance.

  • 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-06-04T14:49:30+00:00Added an answer on June 4, 2026 at 2:49 pm

    There are a variety of ways to set a default value as @Rob mentioned in his comment.

    A callback as @Dave mentioned in his comment is an OK idea, too.

    I suspect the main reason the after_initialize approach isn’t working for you is that you need to explicitly use self as in self.birth = rather than birth =. Ruby thinks you are defining a local variable named birth rather than assigning a value to ActiveRecord’s attribute birth that is implemented via method_missing internally. This is why @pet.birth is nil even though it might appear that you assigned a value to it.

    Also note that the after_initialize callback will be called even for persisted objects when you instantiate them by loading them from the database. It is also called after the attributes are assigned via initialize for new records. Thus to prevent the user-specified value from being trampled on by your default (for both persisted and new records), be sure to do something like this:

    self.birth = value if birth.nil?
    

    Emphasis on the if birth.nil?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.