neves
7/14/2010 - 8:57 PM

rails3_bug.rb

# Declaring:

class Post < ActiveRecord::Base
  has_one :author
  # bug happens only when used the code next line
  accepts_nested_attributes_for :author
end

#Using:

post = Post.new # post.author is nil
form_for(post) do |f|
  f.fields_for(:author) do |af|
    never executed inside here
  end
end

# but doing manually works:
form_for(post) do |f|
  fields_for("post[author_attributes]", post.author) do |af|
    #now works!
  end
end


# New TestCase: actionpack/test/template/form_helper_test.rb

def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association_nil
    @post.author = nil

    assert_deprecated do
      form_for(:post, @post) do |f|
        concat f.text_field(:title)
        concat f.fields_for(:author) { |af|
          concat af.text_field(:name)
        }
      end
    end

    expected = whole_form do
                  '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
                  '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="" />'
                end

    assert_dom_equal expected, output_buffer
  end

"
How to Fix: action_view/helpers/form_helper.rb
Change line 1270 from:
  elsif association
to only:
  else

And change line 1278 from:
  if object.persisted?
to:
  if object.respond_to?(:persisted) && object.persisted?

Doing that, form.fields_for will back behavior as calling direct fields_for.
"