我有 251 个问题的大型心理测试。每个用户都可以多次完成该测试。所以我创建了 Summary 模型来表示每个完成。每个摘要都有许多答案。对于每个摘要,我使用 Slim 模板和 simple_form gem 创建了一个代表答案集合的表单:
= simple_form_for(@summary) do |f|
= f.simple_fields_for :answers do |a|
.question
= a.input :question_id, as: :hidden
div= a.object.question.title
- if a.object.question.kind_of? SpiritualityQuestion
ol class="sortable"
- a.object.question.sortable_variants.each do |sortable_variant|
= content_tag_for :li, sortable_variant
= sortable_variant.title
= a.input :text_data, as: :hidden, input_html: { class: 'sortable_data' }
- elsif a.object.question.kind_of? MultilineQuestion
div Time remaining: <span class="time">60</span> s.
= button_tag 'Start', type: 'button', class: 'start_button btn btn-primary'
= a.input :text_data, label: false, input_html: { class: 'span8 timed_text', cols: '60', rows: '20', disabled: true }
- else
= a.association :variant, collection: a.object.question.variants, as: :radio, label: false
br
= f.input :user_id, as: :hidden
= f.input :psy_test_id, as: :hidden
.actions
= f.button :submit, value: 'Save', class: 'btn btn-large btn-success'
我有相关的控制器动作:
@summary = Summary.where(completed: false, user: current_user, psy_test: PsyTest.first)
.includes(:answers => { :question => :variants })
.first_or_initialize
@summary.build_answers if @summary.new_record?
Summary.build_answers:
def build_answers
# Creating answers for questions
psy_test.questions.includes(:variants).each do |q|
answers.new(question: q)
end
end
现在我正在尝试对测试进行分页,因为它非常大并且表单生成非常缓慢。所以我想为答案添加限制和偏移量。像这样的东西:
= f.simple_fields_for answers.limit(@limit).offset(@offset) do |a|
如何制作?