masta Blasta wrote in post #1071551:
I have two models
term
term_versions (build from acts_as_versioned)

They are related but for all intents and purposes different models.

'term_versions' has this association not available to 'term':
has_many :technical_definitions, :dependent => :destroy
accepts_nested_attributes_for :technical_definitions, :allow_destroy =>
true

When i create a new 'term' i want to also allow for the creation of
technical_definitions related to its versions, but i can't find a good
DRY way of doing it.

The best form i've come up with looks something like this:

<% form_for(@term, ..options, do |f| %>
...
<% fields_for "version[technical_definitions_attributes][]",
new_tech_def do |td_fields| %>
....
<% end %>
<% end %>

The problems with the above are numerous. For one, it's hackish and i
can't reuse that code. Second, it only somewhat works for 1 technical
definition. If i try to give it a collection to work with it goes
haywire. Third, if the model validations fail, and the form has to be
reloaded, those fields are not preserved by the new action, and the
browser loses state. I can get around this last one by adding even more
hackish lines to my controller code.

Working with Rails 2.3 means i don't have the :namespace option on
fields_for which would really help.

Any suggestions fellow devs?
After 5 hours of dealing with this i finally solved it. Mostly a matter
of trying to combine various guides form the web to fit my situation.

The key component was setting up a two level fields_for

<% form_for(@term, ..options, do |f| %>
...
<% f.fields_for :version, @version do |version_fields| %>
<% version_fields.fields_for :technical_definitions do
td_fields| %>
....td_fields.text_field
<% end %>
<% end %>
<% end %>

this set up the correct namespace of:
term[version_attributes][technical_definitions_attributes]

and THEN
my term model gets
attr_accessor :version_attributes

and THEN
the controller gets
@term.version_attributes = processed(params[version_attributes])

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 2 | next ›
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedAug 7, '12 at 2:07p
activeAug 7, '12 at 8:06p
posts2
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

masta Blasta: 2 posts

People

Translate

site design / logo © 2022 Grokbase