我正在尝试创建一个可以记录用户正确答案并显示它们的应用程序。我如何在会话中访问它并显示它。我将计数设置为全局变量,这可能是错误的开始。
我的应用程序询问一个随机问题,如果用户单击正确答案,它会重新加载包含新问题的页面。如果不正确,它会将他们定向到新页面。我将如何显示正确的答案计数?
我的用户和会话设置类似于 michael hartl 的 ruby on rails 教程。
controller.rb
@@ruby_functs_count = 0
def ruby_functs
@symbols = {
"abort" => "Terminates program. If an exception is raised (i.e., $! isn't nil), its error message is displayed.",
"Array( obj)" => "Returns obj after converting it to an array using to_ary or to_a."
}
@random = []
@random2 = []
rando = @symbols.sort_by {rand}
rando = rando.each { |k,v| @random << k and @random2 << v}
@sym = @random2[0]
@answer = @random[0]
@sym2 = @random[0..10].sort_by {rand}
end
sessions_controller.rb
class SessionsController < ApplicationController
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def new
end
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
render 'new'
end
end
ruby_functs.html.erb
<% for @random in @sym2
if @random == @answer
x = cards_ruby_functs_path %>
<li class="btn btn-small btn-info flash"><%= link_to "#{@random}", x %></li>
<% else
x = cards_wrong_path %>
<li class="btn btn-small btn-info flash"><%= link_to "#{@random}", x %></li>
<% end %>
<% end %>