I’m using Rails 3.2.8 and have a set of name/answer pairs for each level in which a user can update:
class UserAnswer < ActiveRecord::Base
attr_accessible :name, :answer, :level_id, :user_id
end
It’s such a pain to create the 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 clearly a hash holding the user’s answers from the last update. There’s so much repetition above. What’s the best way to handle this in Rails? I’d love to use something like form_for, but I don’t think I’m able to because this isn’t a single model object, this is a collection of UserAnswer ActiveRecord instances.
In helpers add:
Then add proper keys to your
en.ymlinconfig/locales. The only thing you need to write is: