Finishing the Question and Question-List Interfaces

After creating the QuestionList and Question controllers, the next step is to update both of them so that defaults are returned when the user edits the extended fields (which conditions the QuqestionList belongs to, and what options a Question has). Because these are stored in other tables, this information is not presented as-saved to the user, but rather the “defaults” are blank.


Populating forms with defaults from the database

With the help of wiki.rubyonrails.org, the process is pretty simple…


… add a new function to question_list_condition.rb (The QuestionListCondition model)

def self.return_checked_by_condition_id_question_list_id(condition_id,question_list_id)
to_find = find_by_condition_id_question_list_id(condition_id,question_list_id)

“checked” if to_find
end

And replace the current checkbox line in views/manage_question_lists/_form.rhtml with:

<br /><%= check_box :selected_condition, condition.id,:checked => QuestionListCondition.return_checked_by_condition_id_question_list_id(condition.id,@question_list.id) %>

to QuestionOption controller will be modified in much the same way

in question_option.rb:

def self.find_display_text_by_question_id_option_id(question_id,option_id)
to_return = find(
:first,
:conditions => {
:question_id => question_id,
:option_id => option_id
}
)

return to_return.display_text if to_return
end

And in app/views/manage_questions/_form.rhtml, just one line needs to be changed:

<%= text_field :question_options, iterator, :value => QuestionOption.find_display_text_by_question_id_option_id(@question.id,iterator) %>

Next up: Display a question list

Leave a Reply

Your email address will not be published. Required fields are marked *