Formatting date field in form

26 views
Skip to first unread message

Robert Fitzpatrick

unread,
Dec 9, 2014, 6:10:50 PM12/9/14
to rubyonra...@googlegroups.com
I am new to RoR and trying to update an existing app developed by someone else. I have the following form in a partial file to display for the new and edit actions. I added :value to the program_date field to display only the date of the timestamp field, removing the time portion of the field value. It works as needed for edit...

= form.inputs do
  = form.input :program, :as => :select, :collection => @programs
  = form.input :headline, :as => :string
  = form.input :message, :as => :text
  = form.input :program_date, :as => :string, :input_html => { :class => 'date-pick', :value => @program_special.program_date.strftime("%m/%d/%Y")}

But when creating a new record, I get an error when trying the same form for a creating a new record, I guess because the field has no value when new...
undefined method `strftime' for nil:NilClass
How can I apply to only the edit form? Or am I trying to accomplish this in the correct way?

Thanks, Robert


--
Robert


-- Robert




Colin Law

unread,
Dec 10, 2014, 3:58:51 AM12/10/14
to rubyonra...@googlegroups.com
You don't need to take different action dependent on whether you are
editting or creating, but, as you have correctly identified, whether
the date has a value or not. You could set the value to a default
value in the new action, possibly something like

@program_special.program_date = Time.now

or you can prevent it trying to display an empty date with something like

= form.input :program_date, :as => :string, :input_html => { :class =>
> 'date-pick', :value => (@program_special.program_date.strftime("%m/%d/%Y") if @program_special.program_date)}

Colin

Robert Fitzpatrick

unread,
Dec 10, 2014, 2:57:00 PM12/10/14
to rubyonra...@googlegroups.com
Robert Fitzpatrick wrote:
> I am new to RoR and trying to update an existing app developed by
> someone else. I have the following form in a partial file to display for
> the new and edit actions. I added :value to the program_date field to
> display only the date of the timestamp field, removing the time portion
> of the field value. It works as needed for edit...
>
> = form.inputs do
> = form.input :program, :as => :select, :collection => @programs
> = form.input :headline, :as => :string
> = form.input :message, :as => :text
> = form.input :program_date, :as => :string, :input_html => { :class =>
> 'date-pick', :value => @program_special.program_date.strftime("%m/%d/%Y")}
>
> But when creating a new record, I get an error when trying the same form
> for a creating a new record, I guess because the field has no value when
> new...
>
> |undefined method `strftime' for nil:NilClass|
>

To clarify, this form is in a partial named _form.html.haml that is used
in the new.html.haml and edit.html.haml files. The new file contains the
following while the edit is the same with the addition of a delete button...

= semantic_form_for(@program_special, :url =>
admin_program_specials_path, :html => {:method => :post, :multipart =>
true, :class => 'well'}) do |f|
= render :partial => 'form', :locals => {:form => f}
= f.actions do
= f.submit 'Save Program Special', :class => 'btn-primary'
= link_to 'Cancel', admin_program_specials_path, :class => 'btn'
--
Robert <rob...@webtent.org>
Reply all
Reply to author
Forward
0 new messages