4

I've a three level nested form, but the third class is not saved.

I've three model classes (simplified)

class A

    has_one :b
    accepts_nested_attributes_for :b

end

and

class B

    belongs_to :a

    has_many :c
    accepts_nested_attributes_for :c

end

and

class C

    belongs_to :b

end

My view (simplified)

<%= form_for [@a] do |f| -%>
    <%= f.fields_for :b do |b_form| -%>
        <%= b_form.fields_for :c do |c_form| -%>
        <% end %>
    <% end %>
<% end %>

The controller

def new
    @a= A.new
    b = @a.b = B.new
    b.c.build
end

def create
    if (@a= A.create(params[:a])).valid?
        //flash succes
    end
end

The hash looks like this: {"a"=>{"title"=>"test", "body"=>"<p>test</p>\r\n<br />", "b_attributes"=>{"title"=>"testt", "c_attributes"=>{"0"=>{"title"=>"testtt"}}}}}

But only A and B are created. C is not, it's not trowing an error or something in my logs..

Thanks!

Edit:

The solution (thanks to Zabba)

add attr_accessible :c_attributes in class B

2
  • 1
    Try adding attr_accessible :c_attributes in class B. Commented May 6, 2011 at 15:36
  • any validations in C and attr_* in B? Commented May 6, 2011 at 15:38

2 Answers 2

2

Try adding attr_accessible :c_attributes in class B

(should make into answer)

Sign up to request clarification or add additional context in comments.

Comments

0

The controller

def new
    @a= A.new
    b= @a.b.build
    b.c.build
end
def create
   @a = A.new(params[:a])
   if @a.valid?
    //flash succes
   end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.