I use Rails 3.2.8 and have a set of name / response pairs for each level at which the user can update:
class UserAnswer < ActiveRecord::Base attr_accessible :name, :answer, :level_id, :user_id end
It’s such a pain to create many views:
<li<%if @error_fields.include?('example_name') or @error_fields.include?('example_other_name')%> class="error_section"<%end%>> <%= label_tag 'answer[example_name]', 'Example question:' %> <%= text_field_tag 'answer[example_name]', @user_answers['example_name'], placeholder: 'Enter answer', class: @error_fields.include?('example_name') ? 'error_field' : '' %> <%= label_tag 'answer[example_other_name]', 'Other example question:' %> <%= text_field_tag 'answer[example_other_name]', @user_answers['example_other_name'], placeholder: 'Enter other answer', class: @error_fields.include?('example_other_name') ? 'error_field' : '' %> </li>
@user_answers is obviously a hash containing user responses from the latest update. There are so many repetitions above. What is the best way to handle this in Rails? I would like to use something like form_for , but I do not think it is possible, because this is not one model object, this is a collection of UserAnswer ActiveRecord instances.
source share