Skip to content

Commit

Permalink
Merge pull request #8 from yoshi3315/test_ajax
Browse files Browse the repository at this point in the history
単語帳機能のajax部分の実装
  • Loading branch information
kawa-mon committed Jan 11, 2019
2 parents bcd71eb + 50950cd commit 7f714f1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
29 changes: 22 additions & 7 deletions app/controllers/tango_tests_controller.rb
Expand Up @@ -3,24 +3,25 @@
class TangoTestsController < ApplicationController
before_action :redirect_non_logged_in_user_to_login_page
before_action :questions_number_valid?
before_action :function_of_remembering_already_used_questions_and_number_of_questions
before_action :scoring

def index
function_of_remembering_already_used_questions_and_number_of_questions

if session[:number_of_questions] >= 50
redirect_to root_url
else
@correct = Question.where.not(id: session[:already_used_questions]).sample
@incorrects = Question.where.not(id: @correct.id).sample(2)
@choices = (@incorrects << @correct).shuffle!
set_question_and_choices
respond_to do |format|
format.html
format.js
end
end
end

private

def questions_number_valid?
msg = t('views.flash.number_danger')
redirect_to root_url, flash: { danger: msg } unless Question.count >= 50
redirect_to root_url, flash: { danger: t('views.flash.number_danger') } unless Question.count >= 50
end

def function_of_remembering_already_used_questions_and_number_of_questions
Expand All @@ -32,4 +33,18 @@ def function_of_remembering_already_used_questions_and_number_of_questions
session[:number_of_questions] = 0
end
end

def scoring
if request.xhr?
session[:correct_answers] += 1 if params[:question_id] == params[:answer_id]
else
session[:correct_answers] = 0
end
end

def set_question_and_choices
@correct = Question.where.not(id: session[:already_used_questions]).sample
@incorrects = Question.where.not(id: @correct.id).sample(2)
@choices = (@incorrects << @correct).shuffle!
end
end
29 changes: 29 additions & 0 deletions app/views/tango_tests/_test_form.html.erb
@@ -0,0 +1,29 @@
<h2 class='text-body text-left mb-3'>
<%= "第#{session[:number_of_questions] + 1}問(全50問)" %>
</h2>

<h2 class='text-body text-left mb-3'>問題</h2>

<div class='mb-3'><%= @correct.question %></div>

<h2 class='text-body text-left mt-3 mb-3'>選択肢</h2>

<div class='mb-3'>
<%= form_with url: tango_tests_path do |f| %>
<% @choices.each.with_index(1) do |choice, index| %>
<div class="form-check">
<% if index == 1%>
<%= f.radio_button :answer_id, choice.id, class: 'form-check-input', checked: true %>
<%= f.label choice.description, class: 'form-check-label' %>
<% else %>
<%= f.radio_button :answer_id, choice.id, class: 'form-check-input' %>
<%= f.label choice.description, class: 'form-check-label' %>
<% end %>
</div>
<% end %>
<%= hidden_field_tag :question_id, @correct.id %>
<%= f.submit '解答', class: 'btn btn-primary' %>
<% end %>
</div>
21 changes: 2 additions & 19 deletions app/views/tango_tests/index.html.erb
@@ -1,22 +1,5 @@
<h1>単語帳</h1>

<h2 class='text-body text-left mb-3'>問題</h2>

<div class='mb-3'><%= @correct.question %></div>

<h2 class='text-body text-left mt-3 mb-3'>選択肢</h2>

<div class='mb-3'>
<%= form_with url: tango_tests_path do |f| %>
<% @choices.each do |choice| %>
<div class="form-check">
<%= f.radio_button :answer_id, choice.id, class: 'form-check-input' %>
<%= f.label choice.description, class: 'form-check-label' %>
</div>
<% end %>
<%= hidden_field_tag :question_id, @correct.id %>
<%= f.submit '仮', class: 'btn btn-primary' %>
<% end %>
<div id='tango-test'>
<%= render 'test_form' %>
</div>
1 change: 1 addition & 0 deletions app/views/tango_tests/index.js.erb
@@ -0,0 +1 @@
$('#tango-test').html("<%= escape_javascript(render('test_form')) %>");

0 comments on commit 7f714f1

Please sign in to comment.