downtroden — 2010-07-22T10:32:27-04:00 — #1
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 'projecttask', i'm trying project_task.due_date. This isn't working. Any help is appreciated.
downtroden — 2010-07-28T10:33:58-04:00 — #2
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
I'll take a look at the stackoverflow page, thanks.
scannon — 2010-07-28T08:54:06-04:00 — #3
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.
Let us know if this works for you or not!
scannon — 2010-07-23T15:21:19-04:00 — #4
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.
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!
downtroden — 2010-07-22T22:31:03-04:00 — #5
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?
downtroden — 2010-07-28T11:38:14-04:00 — #6
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.
downtroden — 2010-07-27T15:14:42-04:00 — #7
The form and date partial are below. I've omitted non important fields/tags for brevity. So in the projecttask 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.
<% f.fields_for :project_tasks do |builder| %>
<%= error_messages_for :project_tasks %>
<%= render "project_task", :f => builder %>
<% end %>
<%= f.label :start_date, "start" %>
<%= f.text_field :start_date, :class => "support_date", :value => project_task %>
Thanks for your help.