Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # id :integer(4) not null, primary key
- # category :string(255)
- # content :text
- class Question < ActiveRecord::Base
- has_many :choices, :dependent => :destroy
- accepts_nested_attributes_for :choices
- end
- # id :integer(4) not null, primary key
- # content :text
- # correct :boolean(1)
- # question_id :integer(4)
- class Choice < ActiveRecord::Base
- belongs_to :question
- end
- def new
- @title = "New Question"
- @question = Question.new
- 3.times { @question.choices.build }
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @question }
- end
- end
- <%= simple_form_for @question do |question_form| %>
- <%= question_form.error_notification %>
- <div class="inputs">
- <%= question_form.input :content, :label => 'Question' %>
- <%= question_form.input :category, :collection => get_categories, :include_blank => false %>
- <% @question.choices.each do |choice| %>
- <%= question_form.fields_for :choices, choice do |choice_fields| %>
- <%= choice_fields.input :content, :label => 'Choice' %>
- <%= choice_fields.radio_button :correct, true %>
- <%= choice_fields.label :correct, 'Correct Answer' %>
- <% end %>
- <% end %>
- </div>
- <div class="actions">
- <%= question_form.button :submit %>
- </div>
- <% end %>
- def create
- @question = Question.new(params[:question])
- # render or redirect stuff....
- end
- <%= choice_fields.radio_button :correct, true, :name => "choices_attributes" %>
- <%= choice_fields.radio_button :correct, true %>
- <%= choice_fields.label :correct, value: true, do %>
- 'True'
- <% end %>
- <%= choice_fields.radio_button :correct, false %>
- <%= choice_fields.radio_button :correct, value: false, do %>
- 'False'
- <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement