【发布时间】:2011-07-14 18:46:36
【问题描述】:
我正在尝试构建一个让您猜数字的游戏。 问题是,如果您犯了错误,它会将您重定向到排行榜(mvc)表单,您可以在其中输入您的姓名,并预先填充来自不同控制器(游戏)的会话数据,然后将两者提交到数据库中。
@round 和@points 是我想要访问并存储为分数和级别的两个变量。
class ApplicationController < ActionController::Base
before_filter :set_current_account
def set_current_account
# set @current_account from session data here
Game.current = @round
end
protect_from_forgery
end
-
class Leaderboard < ActiveRecord::Base
cattr_accessor :current
end
# == Schema Information
#
# Table name: leaderboards
#
# id :integer not null, primary key
# name :string(255)
# score :string(255)
# level :string(255)
# created_at :datetime
# updated_at :datetime
#
-
class GameController < ApplicationController
def index
@games = Game.all
respond_to do |format|
format.html
end
end
def start_game
session[:round] ||= 1
session[:points] ||= 0
@round = session[:round]
@points = session[:points]
end
def generate_round
numbers = Array.new(6){rand(9)}
@addition = []
@display = numbers
numbers.inject do |s, i|
@addition << s + i
@addition.last
end
end
def next_round
session[:round] += 1
session[:points] += 1200
@round = session[:round]
@points = session[:points]
end
def destroy_sessions
session[:round] = nil
session[:points] = nil
session[:addition] = nil
@round = session[:round]
@points = session[:points]
@addition = session[:addition]
start_game
end
def submit_name
@game = Game.new(params[:game])
respond_to do |format|
if @game.save
format.html { redirect_to(leaderboard_path, :notice => 'Score was added successfully.') }
else
format.html { render :action => "new" }
end
end
end
def game_over
redirect_to :controller => 'leaderboards', :action => 'new' and return
end
【问题讨论】:
标签: ruby-on-rails ruby rails-activerecord