Hello,
My ignorance in Rails should be evident from my wording perhaps,
however I couldn''t resolve the following w/o tinkering with
action_view/helpers/form_builder.rb a bit. Please give me some
pointers if possible.
I have a model using globalize2 for translations. It is actually a
has_many assotiation. I am updating the translated attributes through
the nested attributes framework. I am building a form as usual and use
fields_for to show the translated attributes for the different locales
to be edited. So far so good everything works as expected.
However I need to specify the order by which the different locales
appear when fields_for runs. For example I want the :el locale fields
to appear before :en (yes, alphabetically, or more generally, a
predictable ordering is need).
As it is, globalize2 doesn''t use any :order in the assosiaction
definition and that makes sense, this path is called frequently and it
would impose additional strain in the db backend. So the different
locales appear with whatever order they are stored in the database (if
that) which can be considered arbitrary.
I thought of using:
<% f.fields_for :globalize_translations,
f.object.globalize_translations.find(:all, :order => "locale ASC")
do |
t| %> ...
instead but the explicit object is ignored as it is an array and even
then, the objects within won''t respond to new_record method (I am
editing an existing record).
So, any ideas? For the above to work as I''d like, I did this to
actionpack-2.3.2/lib/action_view/helpers/form_builder.rb (arround line
1039):
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
association = @object.send(association_name)
explicit_object = args.first [b] ###### if
args.first.respond_to?(:new_record?)[/b]
if association.is_a?(Array)
children = explicit_object ? [explicit_object].flatten :
association
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
explicit_child_index = args.last[:child_index] if args.last.is_a?
(Hash)
children.map do |child|
fields_for_nested_model("#{name}[#{explicit_child_index ||
nested_child_index}]", child, args, block)
end.join
else
fields_for_nested_model(name, explicit_object || association,
args, block)
end
end
... which of course "works for me" but I have no idea of its side-
effects and of course I feel less than competent about changing core
code like this.
Please help :)
Thanks,
-Kostas