数字を推測するゲームを構築しようとしています。 間違えた場合は、あなたの名前を入力して別のコントローラ(ゲーム)からのセッションデータがあらかじめ入力されており、両方にDBに送信されているリーダーボード(mvc)フォームにリダイレクトされます。異なるコントローラからの変数へのアクセス
@round & @pointsは、スコアとレベルとしてアクセスして保存する2つの変数です。
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
答えをありがとう、私はいくつかのものを交換した後に動作するように見えた。 - > redirect_to:controller => 'leaderboards'、:action => 'new'、:level => session [:round]、:score => session [:points]を返して –
Wootを返します。申し訳ありませんが、どこかからredirect_toを取得してパラメータを追加しました。私はもっと慎重にすべきだった。 :) –