【发布时间】:2015-12-25 08:48:16
【问题描述】:
我是 Ruby on Rails 的新手。我正在尝试构建测验应用程序。有一个“头痛”,一个星期都解决不了。
我选择/选中单选按钮并转到下一个问题,当我回到这个选中的问题或重新加载页面时,选中的问题消失了:我的意思是应用程序没有保存我的选择。我应该使用什么会话, 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
【问题讨论】:
-
不要使用带有bad effect 的会话或cookie。如果他们有很多用户使用该页面,它也会使您的服务器崩溃。在转到下一页之前,您应该保存。
-
如何保存选择?我正在使用 Kaminari 进行分页。
-
你有多少问题在一个问题轮换中。一组 10 个或更多?
-
36 个测试块。但是会有很多不同学科的问题(数学、物理、语言等等..)
-
所以不要保存会话的答案。在继续处理新问题之前,请尝试将它们直接保存到您的数据库中。如果不清楚,请更新您的代码。 (视图+控制器)
标签: ruby-on-rails session cookies radio-button