如何在数据库中保存从索引页获取的值并重定向回索引页?

时间:2018-09-22 12:38:08

标签: ruby-on-rails-5

我是Rails的新手。我正在使用Rails 5.2.1。我正在制作Rails测验应用程序。在“ will_pagnate-bootstrap”的帮助下,我完成了索引页的分页,每页仅显示一个问题。给出答案后,用户将点击保存。我想将用户提供的值存储在数据库中,而无需执行新操作。

仅从索引页开始,它应该带有答案值并应存储在数据库中。另外,我想将页面重定向回显示相同问题的索引页面。而且,我希望单击“保存”后,值应存储在数据库中,并且应与管理员提供的答案匹配,并将结果存储为(“ 0”和“ 1”)或(“正确”和“错误” “)。

用户模型

   class User < ApplicationRecord
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable, :trackable and 
      :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :validatable,
           :confirmable
    has_many :results, dependent: :destroy
  end

结果模型

  class Result < ApplicationRecord
    belongs_to :user
    belongs_to :question
  end

控制器

  class TestQuestionsController < ApplicationController
    before_action :set_admin, only: [:index]
    before_action :set_question_type, only: [:index]
    before_action :set_qid, only: [:create]


    # GET /test_questions
    # GET /test_questions.json
    def index
      @test_questions = @question_type.questions.paginate(:page => 
                        params[:page], :per_page => 1)
    end

    def create
      @test_question =  current_user.results.create 
                        params[resource_params]

      if @test_question.save
        redirect_to test_test_topic_test_question_type_test_questions_path(@subject, @topic, @question_type), notice: 'Response successfully saved. Click on "next"'
      else
        redirect_to test_test_topic_test_question_type_test_questions_path(@subject, @topic, @question_type), alert: 'Response not saved'
      end
    end

    private
      def set_admin
        if admin_signed_in?
          @admin = current_admin
        else
          redirect_to user_index_index_path, alert: 'No test available'
        end
      end

      # Use callbacks to share common setup or constraints between actions.
      def set_question_type
        @subject = current_admin.subjects.find_by_id params[:test_id]
        @topic = @subject.topics.find_by_id params[:test_topic_id]
        @question_type = @topic.question_types.find_by_id params[:test_question_type_id]
      end

    def set_qid
      @subject = current_admin.subjects.find_by_id params[:test_id]
      @topic = @subject.topics.find_by_id params[:test_topic_id]
      @question_type = @topic.question_types.find_by_id params[:test_question_type_id]
      @question = @question_type.questions.find_by_id params[:test_question_id]
    end

    def resource_params
      return [] if request.post?
      [ params.require(:result).permit(:result) ]
    end
  end

索引页

  <h1>Questions</h1>

  <table>
    <thead>
      <tr>
        <th>Questions</th>
        <th></th>
      </tr>
    </thead>

    <tbody>
      <% @test_questions.each do |t| %>
        <tr>
          <td><%= t.question %></td>
        </tr>
        <tr>
          <td>
            <%= form_tag do %>
              <%= hidden_field_tag "question_id", t.id  %>
              <%= radio_button_tag(:result, "true") %>
              <%= label_tag(:result_true, "true") %>
              <%= radio_button_tag(:result, "false") %>
              <%= label_tag(:result_false, "false") %>
              <br>
              <%= submit_tag("Save") %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <br>
  <%= will_paginate @test_questions, renderer: BootstrapPagination::Rails%>
  <br>
  <%= link_to 'Back', test_test_topic_test_question_types_path %>

我尝试向Google回答,但无法解决。我无法保存答案的价值部分。如何存储答案值并使它们与管理员提供的答案匹配。

0 个答案:

没有答案