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.
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_initializeapproach isn’t working for you is that you need to explicitly useselfas inself.birth =rather thanbirth =. Ruby thinks you are defining a local variable namedbirthrather than assigning a value to ActiveRecord’s attributebirththat is implemented viamethod_missinginternally. This is why@pet.birthisnileven 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
initializefor 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:Emphasis on the
if birth.nil?