Bill  Ergashev
Bill Ergashev

Reputation: 43

Radio button selection issue

I'm new to Ruby on Rails. I am trying to build quiz app. There is a "headache" which I can't solve for a week.

I select/check the radio button and go to the next question and when I come back to this checked question or reload the page, checked issue disappears: I mean app isn't saving my selection.What should I use session, cookie or what and how?

index.html.erb

<% @tests.each do |test| %>
    <p><h3><%= @tests.current_page %>. <%= test.question %></h3></p>
        <div class="answers">
            <p>
                <%= collection_radio_buttons(:test, :id, [['A', test.answerA], ['B', test.answerB], ['C', test.answerC], ['D', test.answerD]], :first, :last)  %> 
            </p>
        </div>
<% end %>

tests_controller.rb

def index
    @items = (1..36).to_a
    @tests = Test.all.page(params[:page]).per(1)

    respond_to do |format|
        format.js
        format.html
    end
end

Upvotes: 1

Views: 503

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

I'm new to Ruby on Rails.

Welcome!!!!


You have a structural problem with your application.

The problem is that you're not saving any of your selections in persistent, or even semi-persistent state.

What are these? Here's an explanation; basically it means you're able to save your data either to a file, database or keep it in memory. With traditional apps, this may be in the form of keeping files/RAM available; with web apps, it means db and Redis :)

I could give you information on how to do this, but, honestly, I think the structure of your app needs to improve significantly:


I am trying to build quiz app

To do this, you need to follow the OOP structure of Rails to work with an object. This is what you'll then save, add-to and manipulate. I'll explain in a minute.

#app/models/quiz.rb
class Quiz < ActiveRecord::Base
   has_many :questions
   has_many :answers, through: :questions
end

#app/models/answer.rb
class Answer < ActiveRecord::Base
   #columns id | question_id | user_id | option_id | created_at | updated_at
   belongs_to :question
   belongs_to :user
   belongs_to :option
end

#app/models/option.rb
class Option < ActiveRecord::Base
   belongs_to :question
   has_many :answers
end

#app/models/question.rb
class Question < ActiveRecord::Base
   belongs_to :quiz
   has_many :options
   has_many :answers
end

This will give you the capacity for the following:

#config/routes.rb
resources :quizzes, only: [:show, :index], path: "" do
   resources :questions, only: [:show], path: "" do
      resources :answers, only: [:new, :create] path: "", path_names: { new: "" } #->url.com/quizzes/:quiz_id/:question_id/
   end
end

I've not gone into much depth for this, although I feel that it should give you something to aim at:

#app/controllers/answers_controller.rb
class AnswersController < ApplicationController
   before_action :authenticate_user! #-> assuming you're using devise
   before_action :set_data

   def new
       @answer = @question.answers.new
   end

   def create
       @answer = @question.answers.new answer_params
       @answer.save
   end

   private

   def set_data
      @quiz = Quiz.find params[:quiz_id]
      @question = @quiz.questions.find params[:question_id]
   end

   def answer_params
      params.require(:answer).permit(:option_id).merge(user_id: current_user.id)
   end
end

This gives you the ability to access the following:

#url.com/3/6/

It will yield:

@quiz = Quiz.find "3"
@question = @quiz.questions.find "6"
@answer = @question.answers.new

You'll be able to populate the answers as following:

#app/views/answers/new.html.erb
<%= form_for @answer do |f| %>
   <%= f.collection_select :option, @question.options, :id, :title %>
   <%= f.submit %>
<% end %>

This won't provide you with a paginate-type system (although that can be adapted relatively simply). It will give you the ability to save the responses to your questions in the database.

Here's how:

enter image description here

Rails is built on top of Ruby, both of which inheriting the object orientated nature of the latter.

This means that if you're looking to create any interactivity within your app, you need to consider it from the perspective of objects, not UI flow.

The difference here is that instead of thinking of your "quizzes" as a series of pages, you need to consider how you're going to build, and save, the various objects required to get it to run.

Thus, your question is not about trying to get your particular functionality working, but to ensure you have the objects constructed properly.

Upvotes: 1

Related Questions