Hello all,
I am still very new to programming and wonder if I could get some help.
I have tried to figure this out myself but still having trouble with
some of the concepts. I’ll do my best to explain the problem and
include the code. I may be completely off track, so ANY direction
offered would be deeply appreciated.
I have a question model / object which consists of a question, answers,
and responses to the answers. In the model have the following method
to shuffle the answer –annotation(response) pairs so they don’t look the
same every time they are viewed.
Question.rb model
def set_format_for_presentation
# Create array and shuffle incorrects.
answer_list = [
[incorrect_ans_1, incorrect_anno_1],
[incorrect_ans_2, incorrect_anno_2],
[incorrect_ans_3, incorrect_anno_3],
[incorrect_ans_4, incorrect_anno_4],
[incorrect_ans_5, incorrect_anno_5],
[incorrect_ans_6, incorrect_anno_6]
].shuffle
# Randomly insert the correct answer and response into the
shuffled array.
random_insert = rand(4)
answer_list.insert(random_insert,["#{correct_ans_1} *",
self.correct_anno, self.question_pict])
formatted = {
:anno_1 => answer_list[0][1],
:anno_2 => answer_list[1][1],
:anno_3 => answer_list[2][1],
:anno_4 => answer_list[3][1],
:anno_5 => answer_list[4][1],
:question => self.question,
:answer_a => answer_list[0][0],
:answer_b => answer_list[1][0],
:answer_c => answer_list[2][0],
:answer_d => answer_list[3][0],
:answer_e => answer_list[4][0]
}
formatted
end
In the controller:
@question = Question.find(params[:id])
@formatted_question = @question.set_format_for_presentation
In the view:
<% form_for(@formatted_question, :url => “controller => question”,
:action => “show”) do |f| %>
<%= image_tag @question.question_pict, :alt => "eye.jpg", :size => "470x470", :style=> "border: 3px inset #d7b9c9;" %>
<%= @formatted_question[:question] %>
<%= "A. #{@formatted_question[:answer_a]}" %>
<%= "B. #{@formatted_question[:answer_b]}" %>
<%= "C. #{@formatted_question[:answer_c]}" %>
<%= "D. #{@formatted_question[:answer_d]}" %>
<%= "E. #{@formatted_question[:answer_e]}" %>
<%= render :partial => @anno %>
<%= link_to_remote(
“B”,
:url =>"/questions/#{@ formatted.id}/_anno_2",
:method => “get”,
:update => “ajax_area”) %>
<%= link_to_remote(
“C”,
:url =>"/questions/#{@ formatted.id}/_anno_3",
:method => “get”,
:update => “ajax_area”) %>
<%= link_to_remote(
“D”,
:url =>"/questions/#{@ formatted.id}/_anno_4",
:method => “get”,
:update => “ajax_area”) %>
<%= link_to_remote(
“E”,
:url =>"/questions/#{@ formatted.id}/_anno_5",
:method => “get”,
:update => “ajax_area”) %>
<% end %>
Partial partial_anno_1.html.erb
<%= "#{@formatted_question[:anno_1]}" %>
My goal is to use ajax to display the partial associated with the button
clicked eg click the “A†button and display “_anno_1â€. Cant figure out
how to get the information in the variables to the partial. I cant use
set_format_for_presentation again as it will reshuffle tha attributes
and responses will no longer match the order the displayed answers are
in.
Thanks,
Dave C.