开发者

Problem with saving three level nested form in rails

开发者 https://www.devze.com 2023-03-03 23:54 出处:网络
I\'ve a three level nested form, but the third class is not saved. I\'ve three model classes (simplified)

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"=>{"t开发者_C百科itle"=>"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


Try adding attr_accessible :c_attributes in class B

(should make into answer)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消