Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从不同的控制器访问变量_Ruby On Rails_Ruby_Rails Activerecord - Fatal编程技术网

Ruby on rails 从不同的控制器访问变量

Ruby on rails 从不同的控制器访问变量,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,我想做一个你猜数字的游戏。 问题是,如果您犯了错误,它会将您重定向到排行榜(mvc)表单,在该表单中输入您的姓名,并预先填充来自不同控制器(游戏)的会话数据,然后将两者提交到DB中 @舍入和@points是我想要访问和存储为score和level的两个变量 class ApplicationController < ActionController::Base before_filter :set_current_account def set_current_account

我想做一个你猜数字的游戏。 问题是,如果您犯了错误,它会将您重定向到排行榜(mvc)表单,在该表单中输入您的姓名,并预先填充来自不同控制器(游戏)的会话数据,然后将两者提交到DB中

@舍入和@points是我想要访问和存储为score和level的两个变量

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 ApplicationController
-

class排行榜
-

class GameController“new”}
结束
结束
结束
游戏结束
将_重定向到:controller=>“排行榜”,:action=>“新建”并返回
结束

我认为你在这里采取了一种根本不应该奏效的方法。Rails MVC框架的结构原则是每个请求都是独立服务的,理论上,除了通过传入的
params
、存储在数据库中的记录和持久用户
会话
之外,不存在从一个请求到下一个请求的状态转移

设计一个基于web的应用程序,就像设计一个单进程、单用户、单会话程序一样,是一个错误。使用单例,比如称为
current
cattr\u访问器
,将是有问题的,因为它既在请求之间共享,也在Rails的不同实例之间不共享,其中通常有许多实例

索引
新建
创建
显示
编辑
更新
销毁
的其余标准更紧密地映射将有所帮助。例如,
start\u game
应该是
create
destroy\u sessions
可能应该是
destroy


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

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

  • 将这些值作为参数传递到游戏中

  • 使用此选项重定向

  • 将_重定向到:controller=>'leadboards',:action=>'new'并返回,:round=>params[:round],:points=>params[:points]


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

    我知道我用了错误的方法来构建这整件事。我基本上是尝试将一些纯ruby移植到rails中,并实现各种变通方法来让它工作。在这一点上,我只是想添加最后一项功能。周末我计划坐下来从头开始学习Rails。谢谢你的回答,在我更换了一些东西之后,它似乎起了作用。对于任何正在查看有效内容的人-->重定向到:controller=>'排行榜',:action=>'新',:level=>session[:round],:score=>session[:points]和returnWoot。抱歉,我从某处将重定向_带到了,只是添加了参数。我本应该更小心的
    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