0
2

質問が分岐する診断機能

Posted at

はじめに

この記事では質問が分岐する診断機能の実装をします。
以下イメージ画像のように、回答次第で次の質問内容が変わるように実装していこうと思います。
joukenbun_22599-600x600.jpg

この機能を使って作成したサイト

1.コントローラー設計

コントローラーを分岐させたい質問の数ー1個作ります(一つ目の質問の分岐だけnew.htmlとshow.htmlに書けるため)
今回は質問の分岐の数が4つだった場合を想定して実装していきます。

cmd
$ rails g controller topics
$ rails g controller topic2s
$ rails g controller topic3s
topics_controller.rb
class TopicsController < ApplicationController
    def top
    end
    def new
      @topic = Topic.new
    end
  
    def show
      @topic = Topic.find_by(id: params[:id])
      @topic2 = Topic2.new
    end
  
    def create
      @topic = Topic.new(topic_params)
    #params[:topic][:question] ? @topic.question = params[:topic][:question].join("") : false
      if @topic.save
          flash[:notice] = "診断が完了しました"
          redirect_to topic_path(@topic.id)
      else
          redirect_to :action => "new"
      end
    end
  
  private
    def topic_params
        params.require(:topic).permit(:id, :question)
    end
  
end

他のコントローラーも同じように記述します。topicの数字を変更するのを忘れずに。
showアクションの@topic2 = Topic2.newの部分で質問を次に飛ばしているので、topic3のコントローラーにはこの記述はなくて大丈夫です。

2.データベース周り

コントローラーを作成したのと同じ数のモデルを作ります。

cmd
$ rails g model Topic question:integer
$ rails g model Topic2 question:integer
$ rails g model Topic3 question:integer
cmd
$ rails db:migrate

3.Viewページ周り

topicsのnew.html→topicsのshow.html→topic2sのshow.html→topic3sのshow.htmlの順で診断ページが移動します。

topics/new.html.erb
<p>Q1.一つ目の質問</p>
<%= form_for(@topic, class: 'form-horizontal', role: 'form') do |f| %>
  <%= f.radio_button :question, "1" %>
  <%= label_tag :question_1, "はい" %>
  
  <%= f.radio_button :question, "2" %>
  <%= label_tag :question_2, "いいえ" %>
  <input type="submit" value="次へ">
<% end %>
topics/show.html.erb
<% if @topic.question == 1  %>
  <%= form_for(@topic2) do |f| %>
    <p>Q2.一つ目の質問がはいだった場合</p>
    <%= f.radio_button :question, "3" %>
    <%= label_tag :question_3, "はい" %>

    <%= f.radio_button :question, "4" %>
    <%= label_tag :question_4, "いいえ" %>
  <input type="submit" value="次へ">
  <% end %>
<% end %>

<% if @topic.question == 2  %>
  <%= form_for(@topic2) do |f| %>
    <p>Q2.一つ目の質問がいいえだった場合</p>
    <%= f.radio_button :question, "5" %>
    <%= label_tag :question_5, "はい" %>
    <%= f.radio_button :question, "6" %>
    <%= label_tag :question_6, "いいえ" %>
    <input type="submit" value="次へ">
  <% end %>
<% end %>
topic2s/show.html.erb
<% if @topic2.question == 3  %>
  <%= form_for(@topic3) do |f| %>
    <p>Q3.一つ目の質問がはいで二つ目の質問もだった場合</p>
    <%= f.radio_button :question, "7" %>
    <%= label_tag :question_7, "はい" %>

    <%= f.radio_button :question, "8" %>
    <%= label_tag :question_8, "いいえ" %>
  <input type="submit" value="次へ">
  <% end %>
<% end %>

<% if @topic2.question == 4  %>
  <%= form_for(@topic3) do |f| %>
    <p>Q3.一つ目の質問がはいで二つ目の質問がいいえだった場合</p>
    <%= f.radio_button :question, "9" %>
    <%= label_tag :question_9, "はい" %>
    <%= f.radio_button :question, "10" %>
    <%= label_tag :question_10, "いいえ" %>
    <input type="submit" value="次へ">
  <% end %>
<% end %>

#以下省略

topic2、topic3のshowページもtopicのshow.htmlを参考に書いてみてください。

4.ルート周り

title.rb
#省略
resources :topics
resources :topic2s
resources :topic3s
#省略

以上で完成です。

0
2
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
2