SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reference object attribute in a nested model form

    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.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    @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!

  4. #4
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code Ruby:
    <% f.fields_for :project_tasks do |builder| %>
    	<%= error_messages_for :project_tasks %>
    	<%= render "project_task", :f => builder %>
    <% end %>
    _project_task.html.erb
    Code Ruby:
    <div class="xsmall">
    	<%= f.label :start_date, "start" %>
    	<%= f.text_field :start_date, :class => "support_date", :value => project_task %>
    </div>

    Thanks for your help.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You might be able to do something like the following:

    Code Ruby:
    <%= 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:

    Code:
              
    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/1...t-box-in-rails

    Let us know if this works for you or not! :)

  6. #6
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    undefined method `project_task' for #<ActionView::Helpers::FormBuilder:0x32e19b0>

    project_task.start_date shows this
    Code:
     undefined method `start_date' for nil:NilClass
    f.start_date shows this
    Code:
     undefined method `start_date' for #<ActionView::Helpers::FormBuilder:0x2eda844>
    the f object is this class
    Code:
    ActionView::Helpers::FormBuilder
    I'll take a look at the stackoverflow page, thanks.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  7. #7
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •