Hiall,
I have the following problem: I need to use forms containing
collections (i will call them FCC), and I want to construct them
dynamically, as I need to deal with quite large forms (more than 600
input fields).
The variables @model_name and @field_name contain the proper model and
field names as strings, which is enough for “normal” form input
fields. (i.e. the “if @history_size == 1” path of the code below works
fine!) However, when using FCCs there needs to be a loop over the
instance variable which “contains all the fields” to display input
fields for, as seen in the “else” path of my code.
The problem here is, that @entry and @collection_instance_variable
have to change dynamically! I know the names that they should have,
but I don’t know how to write code, that generates a for loop with
those 2 instance variables properly named. Moreover, it is necessary,
that @entry and @model_name always resolve to the same string, in
order for FCC to work. I guess what is needed here is some
metaprogramming? Unfortunately I have never done that before, and
absolutely don’t know where to start? I would be very glad if anyone
had any tips for me!!
If I didn’t make myself clear with this post, please tell me so! I
will try to explain my problem better if necessary!
Here is the code I’m struggling with. Particularly the “else” path is
the one not working.
<% for @name in @question.ca_question.ca_figure_input_column_names %>
<% @history_size = #fetch size %>
<% @model_name = #fetch name %>
<% @model_id_name = @model_name.classify.to_s.downcase %>
<% @field_name = @name.input_column_name %>
<% if @history_size == 1 %>
<% @id = @model_id_name + “_” + @field_name %>
<%= text_field @model_name, @field_name, { :class => “quest”,
:id => @id } %>
<% else %>
<% for @entry in @collection_instance_variable %>
<%= text_field @model_name + “[]”, @field_name %>
<% end %>
<% end %>
<% end %>
I’m glad about ANY tips!
cheers
Martin