Hi,
I''m working my first small rails app which will allow a user to input
quiz questions and answers, which will then be stored in a database
and output to an xml file which will be loaded into the flash end
product.
I''ve run into a couple of problems. First, I have been trying to use
builder (an rxml file) to generate the xml file. The final xml should
look something like this:
<quiz>
<title>Snappy Reconcile: Zemplar</title>
<libraryButtonEnabled>false</libraryButtonEnabled>
<gamesButtonEnabled>false</gamesButtonEnabled>
<items>
<item>
<question>Which of the following is the reason why it is important
to reconcile Zemplar dosage in Snappy?</question>
<feedback>The answer is C.</feedback>
<answer>To comply with the policy and procedure on no re-entry of
medication vials</answer>
<answer>To satisfy CMS requirement for documentation of Zemplar
vial usage and waste</answer>
<answer correct="y">Both of the above.</answer>
</item>
<item>
<question>The 5 mcg/ml Zemplar concentration comes in 2 vial sizes,
1 ml and 2 ml vials.</question>
<feedback>The answer is A.</feedback>
<answer correct="y">True</answer>
<answer>False</answer>
</item>
</quiz>
So, I''ve only gotten this far in my rxml file before getting stuck:
xml.channel{
for test in @tests
xml.quiz do
xml.title(test.course_title)
xml.libraryButtonEnabled(test.library_button_enabled)
xml.gamesButtonEnabled(test.games_button_enabled)
end
end
}
Here I need to cycle through and list each question, but not all
quizzes will have the same number of questions (between 5 & 20) and
not all questions will have the same number of answers (between 1 &
4). So that''s my first problem.
The second issue I am needing help with is saving the output of that
rxml file into an xml file in the file system. My initial effort was
to define buildxml like this:
def buildxml
@tests = Test.find(:all)
file = File.new("testfile.txt", "rw")
xm = Builder::XmlMarkup.new(STDOUT)
xm.quiz
xm.title(test.title)
xm.gamesButtonEnabled(test.games_button_enabled)
xm.libraryButtonEnabled(test.library_button_enabled)
file.close
flash[:notice] = ''XML file was successfully created.''
redirect_to :action => ''show'', :id => @test
end
Unfortunately, I wasn''t getting anywhere with that - I would get
redirected and the flash message would appear, but there would be no
saved file.
Any help would be greatly appreciated. You might need to take it slow,
I''m a bit of a noob.
Thanks in advance,
--
--
Ian Kennedy
http://www.fiftymillimeter.com
Hi ! Comments below 2005/10/10, Ian Kennedy <exposure@gmail.com>:> <quiz>[snip]> </quiz> > > So, I've only gotten this far in my rxml file before getting stuck: > > xml.channel{ > for test in @tests > xml.quiz do > xml.title(test.course_title) > xml.libraryButtonEnabled(test.library_button_enabled) > xml.gamesButtonEnabled(test.games_button_enabled) > end > end > } > > Here I need to cycle through and list each question, but not all > quizzes will have the same number of questions (between 5 & 20) and > not all questions will have the same number of answers (between 1 & > 4). So that's my first problem.You have an answers model, as well as a questions model, no ? I am guessing that your models look like this: class Quiz has_many :questions end class Question has_many :answers belongs_to :quiz end class Answer belongs_to :question end Then, it's a simple matter to simply iterate over each question and answer: xml.quiz do xml.title(test.course_title) xml.libraryButtonEnabled(test.library_button_enabled) xml.gamesButtonEnabled(test.games_button_enabled) xml.items do test.questions.each do |q| xml.item do xml.question(q.text) xml.feedback(q.feedback) q.answers.each do |a| xml.answer(a.text, 'correct' => a.correct?) end end end end end> The second issue I am needing help with is saving the output of that > rxml file into an xml file in the file system. My initial effort was > to define buildxml like this: > > def buildxml > @tests = Test.find(:all) > file = File.new("testfile.txt", "rw") > xm = Builder::XmlMarkup.new(STDOUT) > xm.quiz > xm.title(test.title) > xm.gamesButtonEnabled(test.games_button_enabled) > xm.libraryButtonEnabled(test.library_button_enabled) > file.close > flash[:notice] = 'XML file was successfully created.' > redirect_to :action => 'show', :id => @test > endWhen creating your Builder, you are telling it to output to STDOUT, not to your new file. Here's what you should have done: def buildxml @tests = Test.find(:all) File.new("testfile.txt", "rw") do |file| xm = Builder::XmlMarkup.new(file) xm.quiz xm.title(test.title) xm.gamesButtonEnabled(test.games_button_enabled) xm.libraryButtonEnabled(test.library_button_enabled) end flash[:notice] = 'XML file was successfully created.' redirect_to :action => 'show', :id => @test end Enjoy ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails