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.
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])
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.