Skip to content

Commit

Permalink
Added new and create actions for multiple choice questions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdflem committed Oct 9, 2018
1 parent 1d90ad3 commit 3599b79
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 93 deletions.
15 changes: 15 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ body {
padding-left: 1.5rem;
padding-right: 1.5rem;
}

div.field {
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}

div.actions {
padding-top: 1.5rem;
padding-bottom: 0.5rem;
}

label,
input {
display: block;
}
15 changes: 15 additions & 0 deletions app/controllers/multiple_choice_questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,19 @@ def show
@multiple_choice_question = MultipleChoiceQuestion.find(params[:id])
# render 'multiple_choice_questions/show.html.erb'
end

def new
@multiple_choice_question = MultipleChoiceQuestion.new
# render 'multiple_choice_questions/new.html.erb'
end

def create
@multiple_choice_question = MultipleChoiceQuestion.new(params.require(:multiple_choice_question).permit(:question, :answer, :distractor_1, :distractor_2, :distractor_3, :distractor_4))
if @multiple_choice_question.save
redirect_to multiple_choice_question_url(@multiple_choice_question), notice: 'Multiple choice question was successfully created.'
else
flash.now[:alert] = 'Error! Unable to create multiple choice question.'
render :new
end
end
end
92 changes: 46 additions & 46 deletions app/views/multiple_choice_questions/ask.html.erb
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@

<main role="main" class="container">

<h1>Answer This Question</h1>

<p>
<%= @multiple_choice_question.question %>
</p>

<%= form_tag do %>
<%
order = [0, 1, 2, 3, 4].shuffle
order.each do |a|
%>
<% if a == 0 %>
<p>
<%= radio_button_tag(:guess, '0') %>
<%= label_tag(:guess_0, @multiple_choice_question.answer) %>
</p>

<% elsif a == 1 %>
<p>
<%= radio_button_tag(:guess, '1') %>
<%= label_tag(:guess_1, @multiple_choice_question.distractor_1) %>
</p>

<% elsif a == 2 %>
<p>
<%= radio_button_tag(:guess, '2') %>
<%= label_tag(:guess_2, @multiple_choice_question.distractor_2) %>
</p>

<% elsif a == 3 %>
<p>
<%= radio_button_tag(:guess, '3') %>
<%= label_tag(:guess_3, @multiple_choice_question.distractor_3) %>
</p>

<% elsif a == 4 %>
<p>
<%= radio_button_tag(:guess, '4') %>
<%= label_tag(:guess_4, @multiple_choice_question.distractor_4) %>
</p>
<h1>Answer This Question</h1>

<p>
<%= @multiple_choice_question.question %>
</p>

<%= form_tag do %>
<%
order = [0, 1, 2, 3, 4].shuffle
order.each do |a|
%>
<% if a == 0 %>
<p>
<%= radio_button_tag(:guess, '0') %>
<%= label_tag(:guess_0, @multiple_choice_question.answer) %>
</p>

<% elsif a == 1 %>
<p>
<%= radio_button_tag(:guess, '1') %>
<%= label_tag(:guess_1, @multiple_choice_question.distractor_1) %>
</p>

<% elsif a == 2 %>
<p>
<%= radio_button_tag(:guess, '2') %>
<%= label_tag(:guess_2, @multiple_choice_question.distractor_2) %>
</p>

<% elsif a == 3 %>
<p>
<%= radio_button_tag(:guess, '3') %>
<%= label_tag(:guess_3, @multiple_choice_question.distractor_3) %>
</p>

<% elsif a == 4 %>
<p>
<%= radio_button_tag(:guess, '4') %>
<%= label_tag(:guess_4, @multiple_choice_question.distractor_4) %>
</p>

<% end %>
<% end %>
<% end %>
<%= submit_tag("Submit Answer") %>
<%= submit_tag("Submit Answer") %>
<% end %>
<% end %>

</main><!-- /.container -->
58 changes: 33 additions & 25 deletions app/views/multiple_choice_questions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@

<h1>Multiple Choice Questions</h1>
<main role="main" class="container">

<table class="table table-sm">
<thead>
<tr>
<th scope="col">Question</th>
<th scope="col">Answer</th>
<th scope="col">Distractor 1</th>
<th scope="col">Distractor 2</th>
<th scope="col">Distractor 3</th>
<th scope="col">Distractor 4</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<% @multiple_choice_questions.each do |question| %>
<h1>Multiple Choice Questions</h1>

<table class="table table-sm">
<thead>
<tr>
<td><%= question.question %></td>
<td><%= question.answer %></td>
<td><%= question.distractor_1 %></td>
<td><%= question.distractor_2 %></td>
<td><%= question.distractor_3 %></td>
<td><%= question.distractor_4 %></td>
<td><%= link_to 'Show', multiple_choice_question_path(question) %></td>
<th scope="col">Question</th>
<th scope="col">Answer</th>
<th scope="col">Distractor 1</th>
<th scope="col">Distractor 2</th>
<th scope="col">Distractor 3</th>
<th scope="col">Distractor 4</th>
<th scope="col"></th>
</tr>
<% end %>
</tbody>
</table>
</thead>
<tbody>
<% @multiple_choice_questions.each do |question| %>
<tr>
<td><%= question.question %></td>
<td><%= question.answer %></td>
<td><%= question.distractor_1 %></td>
<td><%= question.distractor_2 %></td>
<td><%= question.distractor_3 %></td>
<td><%= question.distractor_4 %></td>
<td><%= link_to 'Show', multiple_choice_question_path(question) %></td>
</tr>
<% end %>
</tbody>
</table>

<p>
<%= link_to 'New Multiple Choice Question', new_multiple_choice_question_path %>
</p>

</main><!-- /.container -->
46 changes: 46 additions & 0 deletions app/views/multiple_choice_questions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

<main role="main" class="container">

<h1>New Multiple Choice Question</h1>

<%= form_with model: @multiple_choice_question, local: true do |form| %>
<div class="field">
<%= form.label :question %>
<%= form.text_field :question, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :answer %>
<%= form.text_field :answer, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :distractor_1 %>
<%= form.text_field :distractor_1, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :distractor_2 %>
<%= form.text_field :distractor_2, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :distractor_3 %>
<%= form.text_field :distractor_3, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :distractor_4 %>
<%= form.text_field :distractor_4, class: 'form-control' %>
</div>

<div class="actions">
<%= form.submit 'Create Multiple Choice Question', class: 'btn btn-primary' %>
</div>
<% end %>

<p>
<%= link_to 'Back', multiple_choice_questions_path %>
</p>

</main><!-- /.container -->
46 changes: 25 additions & 21 deletions app/views/multiple_choice_questions/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@

<p>
<b>Question:</b> <%= @multiple_choice_question.question %>
</p>
<main role="main" class="container">

<p>
<b>Answer:</b> <%= @multiple_choice_question.answer %>
</p>
<p>
<b>Question:</b> <%= @multiple_choice_question.question %>
</p>

<p>
<b>Distractor 1:</b> <%= @multiple_choice_question.distractor_1 %>
</p>
<p>
<b>Answer:</b> <%= @multiple_choice_question.answer %>
</p>

<p>
<b>Distractor 2:</b> <%= @multiple_choice_question.distractor_2 %>
</p>
<p>
<b>Distractor 1:</b> <%= @multiple_choice_question.distractor_1 %>
</p>

<p>
<b>Distractor 3:</b> <%= @multiple_choice_question.distractor_3 %>
</p>
<p>
<b>Distractor 2:</b> <%= @multiple_choice_question.distractor_2 %>
</p>

<p>
<b>Distractor 4:</b> <%= @multiple_choice_question.distractor_4 %>
</p>
<p>
<b>Distractor 3:</b> <%= @multiple_choice_question.distractor_3 %>
</p>

<p>
<%= link_to 'Back', multiple_choice_questions_path %>
</p>
<p>
<b>Distractor 4:</b> <%= @multiple_choice_question.distractor_4 %>
</p>

<p>
<%= link_to 'Back', multiple_choice_questions_path %>
</p>

</main><!-- /.container -->
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
get 'multiple_choice_questions/:id/ask', to: 'multiple_choice_questions#ask', as: 'ask_multiple_choice_question'
post 'multiple_choice_questions/:id/ask', to: 'multiple_choice_questions#check_answer'
get 'multiple_choice_questions', to: 'multiple_choice_questions#index', as: 'multiple_choice_questions'
post 'multiple_choice_questions', to: 'multiple_choice_questions#create'
get 'multiple_choice_questions/new', to: 'multiple_choice_questions#new', as: 'new_multiple_choice_question'
get 'multiple_choice_questions/:id', to: 'multiple_choice_questions#show', as: 'multiple_choice_question'

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

0 comments on commit 3599b79

Please sign in to comment.