I’m confused about how to reference the attributes of a nested object in a form view.
I’m wanting to format the date in a specific way and when I try to get the attribute as I normally would, it’s giving me ‘doesn’t exist’ message.
So if the partial is named ‘_project_task’, i’m trying project_task.due_date. This isn’t working. Any help is appreciated.
For some reason it’s not that easy. It’s not seeing the attributes as a normal (what i call) ruby path (object.attribute).
f.project_task.start_date shows this error
undefined method `project_task' for #<ActionView::Helpers::FormBuilder:0x32e19b0>
project_task.start_date shows this
undefined method `start_date' for nil:NilClass
f.start_date shows this
undefined method `start_date' for #<ActionView::Helpers::FormBuilder:0x2eda844>
the f object is this class
ActionView::Helpers::FormBuilder
I’ll take a look at the stackoverflow page, thanks.
You might be able to do something like the following:
<%= f.text_field :start_date, :class => "support_date", :value => f.project_task.start_date.to_s(:date_format) %>
Where :date_format is one of the following:
db ‘%Y-%m-%d’ 2008-08-20
long_ordinal ‘&proc’ August 20th, 2008
long ‘%B %e, %Y’ August 20, 2008
rfc822 ‘%e %b %Y’ 20 Aug 2008
number ‘%Y%m%d’ 20080820
short:‘%e %b’ 20 Aug
You CAN define custom date formats for your app if you need to. (I would have to look it up as it has been awhile since I have done that.)
This thread on StackOverflow may be able to help as well.
http://stackoverflow.com/questions/1422274/format-datetime-in-text-box-in-rails
Let us know if this works for you or not!
I’m not sure that I am understanding exactly what you are trying to do…some code example of what you are trying to do would help me understand.
In a view you can format a date as such.
@cm.startdate.to_s(:long)
The (:long) tells Rails how to format the date.
However, I think you are wanting to do something with a form…so I’ll need more info so I can understand better!
I’ve tried formatting the default date in my environment.rb file, but it seems you have to use the .to_s method with that. What do you do in the case of formatting a text_field with a date in it?
well… problem KIND of solved. This doesn’t feel as clean as I would have hoped (or expect) from rails but basically I created 2 virtual attributes. start_date_string, due_date_string with getter and setter methods. I format the date like I want and voila. I need to come up with a better method name, but it’s working.
I’m not feeling like this is the best answer right now, but it’s working and when I learn more I can always come back.
The form and date partial are below. I’ve omitted non important fields/tags for brevity. So in the _project_task partial, instead of the date (on an edit action) being yyyy-mm-dd, which is rails default…*i want it to list as mm/dd/yyyy. So, with the solution to change the default rails format for the date, i still need to convert the value to a string. So i’m wondering how to access the attributes of the project_task object. BUT what i’m really wanting is to change the way rails displays the date in general so i don’t have to do anything with the actual variable…*all dates for the app just naturally display as mm/dd/yyyy.
_form.html.erb
<% f.fields_for :project_tasks do |builder| %>
<%= error_messages_for :project_tasks %>
<%= render "project_task", :f => builder %>
<% end %>
_project_task.html.erb
<div class="xsmall">
<%= f.label :start_date, "start" %>
<%= f.text_field :start_date, :class => "support_date", :value => project_task %>
</div>
Thanks for your help.