diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index f97d910..7f1a0d0 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -102,6 +102,10 @@ end def permitted_params - params.require(:answer).permit(:id, :plan_id, :user_id, :question_id, :lock_version, :question_option_ids) + permitted = params.require(:answer).permit(:id, :plan_id, :user_id, :question_id, :lock_version, :question_option_ids => []) + if !permitted[:question_option_ids].present? #If question_option_ids has been filtered out because it was a scalar value (e.g. radiobutton answer) + permitted[:question_option_ids] = [params[:answer][:question_option_ids]] # then convert to an Array + end + return permitted end end diff --git a/app/models/answer.rb b/app/models/answer.rb index 54ac937..8267948 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -36,4 +36,10 @@ # # # Make sure the plan and question are associated with the same template! # validates :plan, :question, answer_for_correct_template: true + + # This method helps to decide if an answer option (:radiobuttons, :checkbox, etc ) in form views should be checked or not + # Returns true if the given option_id is present in question_options, otherwise returns false + def has_question_option(option_id) + self.question_option_ids.include?(option_id) + end end diff --git a/app/models/question_option.rb b/app/models/question_option.rb index 84266ab..b0fec82 100644 --- a/app/models/question_option.rb +++ b/app/models/question_option.rb @@ -12,6 +12,7 @@ validates :text, :question, :number, presence: {message: _("can't be blank")} + scope :by_number, -> { order(:number) } ## # deep copy the given question_option and all it's associations # diff --git a/app/views/phases/_answer_form.html.erb b/app/views/phases/_answer_form.html.erb index 6c4eabf..e5408af 100644 --- a/app/views/phases/_answer_form.html.erb +++ b/app/views/phases/_answer_form.html.erb @@ -54,23 +54,27 @@ <% if question.option_based? %> - <% options = question.question_options.order("number") %> + <% options = question.question_options.by_number %> <% if q_format.checkbox? %> - <%= f.input :options, as: :check_boxes, collection: options, label: false, input_html: { id: "options-#{question.id}" } %> +
    + <% options.each do |op| %> +
  1. + <%= f.check_box(:question_option_ids, { multiple: true, checked: answer.has_question_option(op.id) }, op.id, nil) %> + <%= raw op.text %> +
  2. + <% end %> +
<% elsif q_format.multiselectbox? %> <%= f.input :options, as: :select, collection: options, label: false, input_html: { multiple: true , id: "options-#{question.id}" } %> <% elsif q_format.radiobuttons? %>
    <% options.each do |op| %> -
  1. - <% if answer.question_option_ids[0] == op.id then%> - <%= f.radio_button :question_option_ids, op.id, checked: true, id: "answer_option_ids_#{op.id}"%> - <%else%> - <%= f.radio_button :question_option_ids, op.id, checked: false, id: "answer_option_ids_#{op.id}"%> - <% end %> - <%= raw op.text %>
  2. +
  3. + <%= f.radio_button :question_option_ids, op.id, { checked: answer.has_question_option(op.id), id: "answer_option_ids_#{op.id}" } %> + <%= raw op.text %> +
  4. <% end %>
<% elsif q_format.dropdown? %>