2

in my

<%= nested_form_for @object do |f| %>

I've a nested_form like:

<%=f.fields_for :nested, :url => { :action => "new" } do |build| %>
   <%= render 'nested_fields', :f => build %>
<% end %>

and inside that nested_field, I've another fields_for :nested2

My Problem is: I want nested2 appearing 1 time, when nested is called. I tried inside the new action of the nested controller the

@nested = Nested.new
@nested.nested2.build

but this does only work for the "real" new action. Is there any solution for that problem?

I'm using the "nested_form" gem.

2
  • I'm having a bit of trouble understanding exactly what you're asking. Are you trying to create a form for your obkect (typo?), that then has a new nested object in it, with that nested object also have a nested form for a new nested2? Also, what's with the :url => { :action => "new" } in your fields_for call? Commented Mar 8, 2013 at 0:49
  • yes you are right - that was just a typo. I corrected it now. The form is for object and it has the nested inside. This nested can be added as often as I click on the link. For example: I've an invoice with as many articles as I want. To combine them, I use a separate table with the invoice_id and the article_id and also the count of the articles. Now when I want to add a new invoice with new articles, I need to fill out the "count" field in the separate table. Hope now it's a little more clear what I try to do? Commented Mar 8, 2013 at 1:05

1 Answer 1

3

fields_for lets you specify a particular object to render the fields for, so if you want your nested_fields partial to contain nested fields for a single, newly build nested2 model, you can do it in the fields_for call itself, like this:

# '_nested_fields.html.erb'

...
<%= f.fields_for :nested2, f.object.build_nested2 do |build| %>
  <%= ... %>
<% end %>

This is assuming that Nested has_one :nested2, if it's a has_many association the fields_for arguments would be slightly different:

<%= f.fields_for :nested2s, f.object.nested2s.build do |build| %>

f.object allows you to access the form builder's object, and you can then use it's association methods (based on the association type) to build the new object at that point.

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

13 Comments

In my case it's a has_many association. What do I have to change at this argument?
I've updated my answers to cover either situation. From your description above though, I don't quite understand why you'd need a has_many association (although I think I'm still missing something about what you're trying to achieve).
It's nearly exact the situation above I'm trying to realise. I've the objects invoices, articles and "invoice_articles". The last one has "belongs_to" accociations and the first two has both "has_many :through" associations. Your changed solution raises the following exception: undefined method `invoice_articles'
So Invoice has_many :articles, through: :invoice_articles, and Article has_many :invoices, through: :invoice_articles? Do they both have has_many :invoice_articles?
exactly - that's my actual association schema. And in invoice and article, I added "accepts_nested_attributes_for :invoice_articles" as well
|

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.