单选按钮选择问题
Radio button selection issue
我是 Rails 上 Ruby 的新手。我正在尝试构建测验应用程序。有个"headache"一个星期都解决不了。
我 select/check 单选按钮并转到下一个问题,当我回到这个选中的问题或重新加载页面时,选中的问题消失了:我的意思是应用程序没有保存我的 selection.What 我应该使用会话、cookie 还是什么以及如何使用?
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
I'm new to Ruby on Rails.
欢迎光临!!!
您的应用程序存在结构问题。
问题是您没有在 persistent
甚至 semi-persistent
状态下保存任何选择。
这些是什么? 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 am trying to build quiz app
为此,您需要遵循 Rails 的 OOP
结构来处理 对象 。这就是您随后要保存、添加和操作的内容。我待会儿解释。
#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
这将为您提供以下能力:
#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
虽然我觉得它应该给你一些目标:
我没有对此进行深入探讨:
#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
这使您能够访问以下内容:
#url.com/3/6/
它将产生:
@quiz = Quiz.find "3"
@question = @quiz.questions.find "6"
@answer = @question.answers.new
您将能够按照以下方式填充答案:
#app/views/answers/new.html.erb
<%= form_for @answer do |f| %>
<%= f.collection_select :option, @question.options, :id, :title %>
<%= f.submit %>
<% end %>
这不会为您提供分页类型的系统(尽管可以相对简单地进行调整)。它 将 使您能够将对问题的答复保存在数据库中。
方法如下:
Rails建立在Ruby之上,两者都继承了面向对象的特性后者。
这意味着如果您希望在您的应用程序中创建任何交互性,您需要从对象[=72=的角度来考虑它],不是UI流量。
这里的不同之处在于,您无需将 "quizzes" 视为一系列页面,而是需要考虑如何构建和保存将其传送到 运行.
因此,您的问题不是尝试让您的特定功能正常工作,而是确保您正确构造了对象。
我是 Rails 上 Ruby 的新手。我正在尝试构建测验应用程序。有个"headache"一个星期都解决不了。
我 select/check 单选按钮并转到下一个问题,当我回到这个选中的问题或重新加载页面时,选中的问题消失了:我的意思是应用程序没有保存我的 selection.What 我应该使用会话、cookie 还是什么以及如何使用?
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
I'm new to Ruby on Rails.
欢迎光临!!!
您的应用程序存在结构问题。
问题是您没有在 persistent
甚至 semi-persistent
状态下保存任何选择。
这些是什么? 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 am trying to build quiz app
为此,您需要遵循 Rails 的 OOP
结构来处理 对象 。这就是您随后要保存、添加和操作的内容。我待会儿解释。
#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
这将为您提供以下能力:
#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
虽然我觉得它应该给你一些目标:
我没有对此进行深入探讨:#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
这使您能够访问以下内容:
#url.com/3/6/
它将产生:
@quiz = Quiz.find "3"
@question = @quiz.questions.find "6"
@answer = @question.answers.new
您将能够按照以下方式填充答案:
#app/views/answers/new.html.erb
<%= form_for @answer do |f| %>
<%= f.collection_select :option, @question.options, :id, :title %>
<%= f.submit %>
<% end %>
这不会为您提供分页类型的系统(尽管可以相对简单地进行调整)。它 将 使您能够将对问题的答复保存在数据库中。
方法如下:
Rails建立在Ruby之上,两者都继承了面向对象的特性后者。
这意味着如果您希望在您的应用程序中创建任何交互性,您需要从对象[=72=的角度来考虑它],不是UI流量。
这里的不同之处在于,您无需将 "quizzes" 视为一系列页面,而是需要考虑如何构建和保存将其传送到 运行.
因此,您的问题不是尝试让您的特定功能正常工作,而是确保您正确构造了对象。