Tawnos
2[H]4U
- Joined
- Sep 9, 2001
- Messages
- 3,808
If the topic didn't scare you away, thanks .
As part of a web app I'm writing, I'm creating a multiple choice test creation app.
The database is set up as such:
The model for questions (question.rb) is like so:
A Question must have at least one valid answer, which is the answer field from above.
The wrong answer (wrong_answer.rb) model:
I'd like the question creation to function so that there's two text input boxes, question and answer. They define the parent object's question and answer. Below those boxes is another, that allows the administrator to add a wrong answer. Adding the answer should be handled in memory, with no database update (as there is no question created until the answer is committed). Upon being added, the answer should appear in a text list above the edit box, with an "edit" and "delete" option. "Edit" turns the text into an edit box, allowing one to change the answer or delete it. "Delete" removes it from the in-memory database, or the real database if this is a question that has been opened for editing.
To this end, I have the following:
new.rhtml
_wrong_answer_editbox.rhtml
The text partial is similar to the edit box. The problem i'm having is this: how do I make the controller create an in memory WrongAnswer, associated with the uncreated question. Moreover, how do I transfer the lsit from memory to database?
I know this may be unclear. I'm sorry, please ask any needed questions for clarification and I'll do my best.
As part of a web app I'm writing, I'm creating a multiple choice test creation app.
The database is set up as such:
Code:
create table questions (
id int not null auto_increment,
question varchar(250) not null,
answer varchar(100) not null,
wrong_answers_count int default 0,
primary key (id)
);
create table wrong_answers (
id int not null auto_increment,
question_id int not null,
answer varchar(100) not null,
constraint fk_question foreign key (question_id) references questions(id),
primary key (id)
);
The model for questions (question.rb) is like so:
Code:
class Question < ActiveRecord::Base
has_and_belongs_to_many :tests
has_many :wrong_answers,
:exclusively_dependent => true
validates_presence_of :question, :answer
end
The wrong answer (wrong_answer.rb) model:
Code:
class WrongAnswer < ActiveRecord::Base
belongs_to :question, :counter_cache => true
validates_presence_of :answer
end
I'd like the question creation to function so that there's two text input boxes, question and answer. They define the parent object's question and answer. Below those boxes is another, that allows the administrator to add a wrong answer. Adding the answer should be handled in memory, with no database update (as there is no question created until the answer is committed). Upon being added, the answer should appear in a text list above the edit box, with an "edit" and "delete" option. "Edit" turns the text into an edit box, allowing one to change the answer or delete it. "Delete" removes it from the in-memory database, or the real database if this is a question that has been opened for editing.
To this end, I have the following:
new.rhtml
Code:
<h1>New question</h1>
<%= start_form_tag :action => 'create' %>
<%= render :partial => 'form' %>
<br />Wrong Answers:<br />
<div id="wrong_answer_list">
</div>
<%= render(:partial => 'wrong_answer_editbox') %>
<br />
<%= submit_tag "Create" %>
<%= end_form_tag %>
<%= link_to 'Back', :action => 'list' %>
_wrong_answer_editbox.rhtml
Code:
<!-- BEGIN _wrong_answer_editbox.rhtml -->
<%= form_remote_tag(:url => { :action => "add_wrong_answer" },
:position => :top) %>
<%= text_field_tag('wrong_answer', '', :id => 'wrong_answer_body') %>
<%= link_to_remote("Add",
:update => "wrong_answer_list",
:url => { :action => "add_wrong_answer" }) %>
<%= end_form_tag %>
<!-- END _wrong_answer_editbox.rhtml -->
The text partial is similar to the edit box. The problem i'm having is this: how do I make the controller create an in memory WrongAnswer, associated with the uncreated question. Moreover, how do I transfer the lsit from memory to database?
I know this may be unclear. I'm sorry, please ask any needed questions for clarification and I'll do my best.