Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. # id :integer(4) not null, primary key
  2. # category :string(255)
  3. # content :text
  4. class Question < ActiveRecord::Base
  5. has_many :choices, :dependent => :destroy
  6. accepts_nested_attributes_for :choices
  7. end
  8.  
  9. # id :integer(4) not null, primary key
  10. # content :text
  11. # correct :boolean(1)
  12. # question_id :integer(4)
  13. class Choice < ActiveRecord::Base
  14. belongs_to :question
  15. end
  16.  
  17. def new
  18. @title = "New Question"
  19. @question = Question.new
  20. 3.times { @question.choices.build }
  21.  
  22. respond_to do |format|
  23. format.html # new.html.erb
  24. format.xml { render :xml => @question }
  25. end
  26. end
  27.  
  28. <%= simple_form_for @question do |question_form| %>
  29. <%= question_form.error_notification %>
  30.  
  31. <div class="inputs">
  32. <%= question_form.input :content, :label => 'Question' %>
  33. <%= question_form.input :category, :collection => get_categories, :include_blank => false %>
  34.  
  35. <% @question.choices.each do |choice| %>
  36. <%= question_form.fields_for :choices, choice do |choice_fields| %>
  37. <%= choice_fields.input :content, :label => 'Choice' %>
  38. <%= choice_fields.radio_button :correct, true %>
  39. <%= choice_fields.label :correct, 'Correct Answer' %>
  40. <% end %>
  41. <% end %>
  42. </div>
  43.  
  44. <div class="actions">
  45. <%= question_form.button :submit %>
  46. </div>
  47. <% end %>
  48.  
  49. def create
  50. @question = Question.new(params[:question])
  51. # render or redirect stuff....
  52. end
  53.  
  54. <%= choice_fields.radio_button :correct, true, :name => "choices_attributes" %>
  55.  
  56. <%= choice_fields.radio_button :correct, true %>
  57. <%= choice_fields.label :correct, value: true, do %>
  58. 'True'
  59. <% end %>
  60. <%= choice_fields.radio_button :correct, false %>
  61. <%= choice_fields.radio_button :correct, value: false, do %>
  62. 'False'
  63. <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement