0
0

診断機能(ポイント制)

Posted at

Ruby on Rails でポイント制の診断機能を実装する。
おすすめのキャラクターの診断ができる機能を実装する。

開発環境

  • Ruby3.0.6

診断設計

Question1からQuestion3に対して「はい」→キャプテンアメリカに+1
Question4からQuestion6に対して「はい」→アイアンマンに+1
Question7からQuestion9に対して「はい」→スパイダーマンに+1
Question10からQuestion12に対して「はい」→ソーに+1
計12問の「はい/いいえ」の質問に回答すると、「はい」の数が最も多いキャラクターが表示される。

手順

  1. モデル
  2. ルーティング
  3. コントローラー
  4. ビュー

1. モデル

question1からquestion12の計12個(データ型がinteger)のカラムを持つTestモデルを作成する。

rails g model Test question1:integer question2:integer ...
rails db:migrate

2. ルーティング

resourcesでルーティングする。

routes.rb
resources :tests

3. コントローラー

testsコントローラーを作成する。

rails g controller tests

コントローラーの中でアクションを定義する。

tests_controller.rb
class TestsController < ApplicationController
    def index
    end

    def new
        @test = Test.new
    end

    def show
        @test = Test.find_by(id: params[:id])
    end

    def create
        @test = Test.new(test_params)
        
        if @test.save
            flash[:notice] = "診断が完了しました"
            redirect_to test_path(@test.id)
        else
            redirect_to :action => "new"
        end
    end

    private
        def test_params
            params.require(:test).permit(:id, :question1, :question2, :question3, :question4, :question5, :question6, :question7, :question8, :question9, :question10, :question11, :question12)
        end
end

4. ビュー

indexのビューを作成する。

index.html.erb
<h1>キャラクター診断</h1>

<div class="start">
    <h6>Are you ready?</h6>
    <%= link_to "診断を始める", new_test_path, class: "link" %>
</div>

診断の回答をするページ(newのビュー)を作成する。

new.html.erb
<h4>12問に回答して、おすすめのキャラクターを知ろう!</h4>
<%= form_for(@test, class: 'form-horizontal', role: 'form') do |f| %>
    <div class="container-sindan">
        <div class="row">
            <div class="col-12">
                <p>Question1. 理論よりも情熱の方が大事だと思いますか?</p>
            </div>
            <div class="col-6">
                <p><%= f.radio_button :question1, :"1" %>はい</p>
            </div>
            <div class="col-6">
                <p><%= f.radio_button :question1, :"0" %>いいえ</p>
            </div>

            <div class="col-12">
                <p>Question2. ルールを破る人は許せませんか?</p>
            </div>
            <div class="col-6">
                <p><%= f.radio_button :question2, :"1" %>はい</p>
            </div>
            <div class="col-6">
                <p><%= f.radio_button :question2, :"0" %>いいえ</p>
            </div>

            # 同じように残り10問分も作る。
        </div>
    </div>
    <div class="sindanbotan">
        <%= f.submit "post" %>
    </div>
<% end %>

結果を表示するページ(showのビュー)を作成する。

show.html.erb
<div class="intoro">
<h5>診断結果</h5>
<p>あなたにおすすめのキャラクターは…</p>
</div>
<div class="tweet-container">
    <div class="tweet">
        <% if @test.question1 + @test.question2 + @test.question3 >= 3 %>
            <h3>キャプテンアメリカ</h3>
        <% elsif @test.question4 + @test.question5 + @test.question6 >= 3 %>
            <h3>アイアンマン</h3>
        <% elsif @test.question7 + @test.question8 + @test.question9 >= 3 %>
            <h3>スパイダーマン</h3>
        <% elsif @test.question10 + @test.question11 + @test.question12 >= 3 %>
            <h3>ソー</h3>

        <% elsif @test.question1 + @test.question2 + @test.question3 >= 2 %>
            <h3>キャプテンアメリカ</h3>
        <% elsif @test.question4 + @test.question5 + @test.question6 >= 2 %>
            <h3>アイアンマン</h3>
        <% elsif @test.question7 + @test.question8 + @test.question9 >= 2 %>
            <h3>スパイダーマン</h3>
        <% elsif @test.question10 + @test.question11 + @test.question12 >= 2 %>
            <h3>ソー</h3>
        
        <% elsif @test.question1 + @test.question2 + @test.question3 >= 1 %>
            <h3>キャプテンアメリカ</h3>
        <% elsif @test.question4 + @test.question5 + @test.question6 >= 1 %>
            <h3>アイアンマン</h3>
        <% elsif @test.question7 + @test.question8 + @test.question9 >= 1 %>
            <h3>スパイダーマン</h3>
        <% elsif @test.question10 + @test.question11 + @test.question12 >= 1 %>
            <h3>ソー</h3>

        <% else %>
            <h3>おすすめなし</h3>
        <% end %>
    </div>
</div>

<%= link_to "診断をやり直す", tests_path %>
<%= link_to "投稿一覧へ", lovers_path %>

以上で完成!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0