SHOW:
|
|
- or go back to the newest paste.
1 | ## associantions model | |
2 | ||
3 | class Quiz < ActiveRecord::Base | |
4 | - | attr_accessible :title, :jumlahpertanyaan, :jumlahjawaban |
4 | + | attr_accessible :title, :jumlahpertanyaan, :jumlahjawaban, :pertanyaans_attributes |
5 | has_many :pertanyaans, :dependent => :destroy | |
6 | accepts_nested_attributes_for :pertanyaans | |
7 | end | |
8 | class Pertanyaan < ActiveRecord::Base | |
9 | attr_accessible :content, :quiz_id, :jawabans_attributes | |
10 | belongs_to :quiz | |
11 | has_many :jawabans, :dependent => :destroy | |
12 | accepts_nested_attributes_for :jawabans | |
13 | end | |
14 | ||
15 | class Jawaban < ActiveRecord::Base | |
16 | attr_accessible :content, :correct, :pertanyaan_id | |
17 | belongs_to :pertanyaan | |
18 | end | |
19 | ||
20 | ||
21 | ## Step Pertama | |
22 | ||
23 | ## quizzes_controller.rb | |
24 | ||
25 | def new | |
26 | @quiz = Quiz.new | |
27 | end | |
28 | ||
29 | def create | |
30 | @quiz = Quiz.new(params[:quiz]) | |
31 | if @ujian.save | |
32 | redirect_to quiz_steps_path(:quiz_id => @quiz.id), notice: "Silahkan buat pertanyaann untuk " + @quiz.nama + "." | |
33 | else | |
34 | render :new | |
35 | end | |
36 | end | |
37 | ||
38 | ## form quizzes/new.html.erb | |
39 | ||
40 | <%= form_for @ujian, :url => create_ujian_subdomain_path(@ujian), :method => :post do |f| %> | |
41 | ||
42 | <div class="field"> | |
43 | <%= f.label :title %><br /> | |
44 | <%= f.text_field :title %> | |
45 | </div> | |
46 | <div class="field"> | |
47 | <%= f.label :jumlahpertanyaan %><br /> | |
48 | <%= f.text_field :jumlahpertanyaan %> | |
49 | </div> | |
50 | <div class="field"> | |
51 | <%= f.label :jumlahjawaban %><br /> | |
52 | <%= f.text_field :jumlahjawaban %> | |
53 | </div> | |
54 | <div class="actions"> | |
55 | <%= f.submit ( "Next" ), :class => "btn btn-inverse btn-medium" %> | |
56 | </div> | |
57 | <% end %> | |
58 | ||
59 | ||
60 | ## Step Kedua | |
61 | ||
62 | ## quiz_steps_controller.rb | |
63 | include Wicked::Wizard | |
64 | steps :pertanyaan | |
65 | def show | |
66 | @quiz = Quiz.find(params[:ujian_id]) | |
67 | jumpertanyaan = @quiz.jumlahpertanyaan | |
68 | jumjawaban = @quiz.jumlahjawaban | |
69 | jumpertanyaan.times do | |
70 | pertanyaan = @quiz.pertanyaans.build | |
71 | jumjawaban.times { pertanyaan.jawabans.build } | |
72 | end | |
73 | render_wizard | |
74 | end | |
75 | ||
76 | def update | |
77 | @quiz = Quiz.find(params[:quiz]) | |
78 | @quiz_attributes = (params[:quiz]) | |
79 | render_wizard @quiz | |
80 | end | |
81 | ||
82 | ||
83 | ## form pertanyaan.html.erb | |
84 | ||
85 | <%= form_for @quiz, :url => wizard_path do |f| %> | |
86 | <fieldset> | |
87 | <%= f.fields_for :pertanyaans do |builder| %> | |
88 | <%= render "form_pertanyaans", :f => builder %> | |
89 | <% end %> | |
90 | </fieldset> | |
91 | <div class="actions"> | |
92 | <%= f.submit ( "Simpan" ), :class => "btn btn-inverse btn-medium" %> | |
93 | </div> | |
94 | <% end %> |