app/models/question.rb
def save_answers(params, answer, other_text, fields_to_clear, details_fields)
    saved_something = false
    if other_text
      #check if the "other" field has been selected.
      # If params is an hash we are dealing with a check-boxes answer type and the 'other' is selected if params["-1"].present?
      # If params is a String its either a radio button or a drop down or a select box. So if its a radio button params should be equal to "-1" for 'other' to be selected. For drop down or select box other_text just needs to be presented (not blank)
      if ( (params.is_a?(Hash) && !params["-1"].present? ) || (params.is_a?(String) && self.answer_type.display_type != 1 && params != "-1" )) &&  !other_text.blank?
        answer.other_text = ""
        answer.save!
        saved_something = true
        fields_to_clear << "answer_#{answer.question_id.to_s + (answer.looping_identifier.present? ? "_#{answer.looping_identifier}" : "")}_other"
      else
        if answer.other_text != other_text
          answer.other_text = other_text
          answer.save!
          saved_something = true
        end
      end
    end
    if self.answer_type.is_a?(MultiAnswer)
      self.answer_type.save_answers(params, answer, details_fields, fields_to_clear) || saved_something
    else
      self.answer_type.save_answers(params, answer) || saved_something
    end
  end
# Function to save the answers of a question
# Params:
# params (Hash or String): the data that has been input for this question.id + identifier (if it exists) . params is an hash when the answer type is MultiAnswer and the selection type is multiple. Otherwise it will be a string.
# answer (Answer): the answer object that will store the answer for this question+identifier . Is unique for [user.id, question.id, looping_identifier]
# other_text (string): For Multi Answer questions an "other"  option might exist. the variable other_text holds its value
# fields_to_clear (hash): hash with the form id of the other_text, if it is to be cleared (when the other option is not selected)
# details_fields (hash): For multiAnswers options can have a "details field", the data that has been input in those fields comes in this variable. An hash :multi_option_id => :details_text
Function to save the answers of a question
Params:
params (Hash or String): the data that has been input for this question.id + identifier (if it exists) . params is an hash when the answer type is MultiAnswer and the selection type is multiple. Otherwise it will be a string.
answer (Answer): the answer object that will store the answer for this question+identifier . Is unique for [user.id, question.id, looping_identifier]
other_text (string): For Multi Answer questions an "other"  option might exist. the variable other_text holds its value
fields_to_clear (hash): hash with the form id of the other_text, if it is to be cleared (when the other option is not selected)
details_fields (hash): For multiAnswers options can have a "details field", the data that has been input in those fields comes in this variable.
Function to save the answers of a question Params : params ( Hash or String ) : the data that has been input for this question . id + identifier ( if it exists ) . params is an hash when the answer type is MultiAnswer and the selection type is multiple . Otherwise it will be a string . answer ( Answer ) : the answer object that will store the answer for this question + identifier . Is unique for [ user . id question . id looping_identifier ] other_text ( string ) : For Multi Answer questions an " other " option might exist . the variable other_text holds its value fields_to_clear ( hash ) : hash with the form id of the other_text if it is to be cleared ( when the other option is not selected ) details_fields ( hash ) : For multiAnswers options can have a " details field " the data that has been input in those fields comes in this variable .
def save_answers(params, answer, other_text, fields_to_clear, details_fields)
    saved_something = false
    if other_text
      if ( (params.is_a?(Hash) && !params["-1"].present? ) || (params.is_a?(String) && self.answer_type.display_type != 1 && params != "-1" )) &&  !other_text.blank?
        answer.other_text = ""
        answer.save!
        saved_something = true
        fields_to_clear << "answer_#{answer.question_id.to_s + (answer.looping_identifier.present? ? "_#{answer.looping_identifier}" : "")}_other"
      else
        if answer.other_text != other_text
          answer.other_text = other_text
          answer.save!
          saved_something = true
        end
      end
    end
    if self.answer_type.is_a?(MultiAnswer)
      self.answer_type.save_answers(params, answer, details_fields, fields_to_clear) || saved_something
    else
      self.answer_type.save_answers(params, answer) || saved_something
    end
  end
def save_answers ( params , answer , other_text , fields_to_clear , details_fields ) saved_something = false if other_text if ( ( params . is_a? ( Hash ) && ! params [ "-1" ] . present? ) || ( params . is_a? ( String ) && self . answer_type . display_type != 1 && params != "-1" ) ) && ! other_text . blank? answer . other_text = "" answer . save! saved_something = true fields_to_clear << "answer_#{answer.question_id.to_s + (answer.looping_identifier.present? ? "_#{answer.looping_identifier}" : "")}_other" else if answer . other_text != other_text answer . other_text = other_text answer . save! saved_something = true end end end if self . answer_type . is_a? ( MultiAnswer ) self . answer_type . save_answers ( params , answer , details_fields , fields_to_clear ) || saved_something else self . answer_type . save_answers ( params , answer ) || saved_something end end
Function to save the answers of a question
Params:
params (Hash or String): the data that has been input for this question.id + identifier (if it exists) .
Function to save the answers of a question Params : params ( Hash or String ) : the data that has been input for this question . id + identifier ( if it exists ) .
#check if the "other" field has been selected. # If params is an hash we are dealing with a check-boxes answer type and the 'other' is selected if params["-1"].present? # If params is a String its either a radio button or a drop down or a select box. So if its a radio button params should be equal to "-1" for 'other' to be selected. For drop down or select box other_text just needs to be presented (not blank)
params
answer
other_text
fields_to_clear
details_fields