Ruby on Rails Wednesday, December 10, 2014

On 9 December 2014 at 23:08, Robert Fitzpatrick <robert@webtent.org> 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
>
> How can I apply to only the edit form? Or am I trying to accomplish this in
> the correct way?

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuU4Vvr%3D_zYv5omAKv%2B_wf%2BiUoL3vaGCUJmF%3DTnGww7Uw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment