【问题标题】:Accessing variables from different controllers从不同的控制器访问变量
【发布时间】: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


    【解决方案1】:

    我还没有阅读全文,但如果您只想访问这些变量,您可以将它们作为参数传递。

    1. 将这些值作为参数传递给 game_over

    2. 使用它来重定向

    redirect_to :controller => 'leaderboards', :action => 'new' 并返回, :round => params[:round], :points => params[:points]

    或者,您可以只保留会话,直到开始新游戏或将分数记录到排行榜。

    【讨论】:

    • 感谢您的回答,我更换了一些东西后似乎可以正常工作。对于任何寻找有效方法的人 --> redirect_to :controller => 'leaderboards', :action => 'new', :level => session[:round], :score => session[:points] 并返回
    • 哇。抱歉,我从某个地方获取了 redirect_to 并添加了参数。我应该更加小心。 :)
    【解决方案2】:

    我认为您在这里采取了一种根本行不通的方法。 Rails MVC 框架是围绕每个请求独立服务的原则构建的,理论上,除了通过 params 传入、存储在数据库中的记录和持久用户之外,没有从一个请求到下一个请求的状态转移session.

    像您这样设计一个基于 Web 的应用程序可能是一个单进程、单用户、单会话程序是错误的。使用单例,比如你的 cattr_accessor 称为 current 将会有问题,因为它在请求之间共享,而不是在不同的 Rails 实例之间共享,其中通常有很多。

    更接近于indexnewcreateshoweditupdatedestroy 的 REST 标准会有所帮助。例如start_game 应该是createdestroy_sessions 应该是destroy

    您的设计并不清楚每个游戏是由多个用户共享,还是为每个用户单独创建,因此很难说更多关于如何解决您的问题。

    【讨论】:

    • 我知道我ve taken the wrong approach for building this whole thing. I basically tried porting something pure ruby in to rails and implementing various workarounds just to get it working. At this point I just wand to add this last piece of functionality. Over the weekend Im 打算坐下来从头开始学习 Rails。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2011-12-13
    相关资源
    最近更新 更多