Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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 什么时候调用应用程序\u控制器中的\u过滤器之前?奇怪的问题_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 什么时候调用应用程序\u控制器中的\u过滤器之前?奇怪的问题

Ruby on rails 什么时候调用应用程序\u控制器中的\u过滤器之前?奇怪的问题,ruby-on-rails,ruby,Ruby On Rails,Ruby,我开发了一个插件。插件有一个声明,可以添加到应用程序控制器,如下所示: class ApplicationController < ActionController::Base set_something_to(Account.first) end class ApplicationController

我开发了一个插件。插件有一个声明,可以添加到应用程序控制器,如下所示:

class ApplicationController < ActionController::Base

  set_something_to(Account.first)
end
class ApplicationController
一切如期进行。但是,当我使用before_过滤器动态获取要设置的值时,如下所示:

class ApplicationController < ActionController::Base

  before_filter :get_account
  set_something_to(@account)

  protected
    def get_account
      @account = Account.first
    end
end
class ApplicationController
这是行不通的。传递给
set\u something\u to
声明的值为nil。为什么值为零?动态传递值的正确方法是什么

谢谢你抽出时间。
Erwin

在每次操作之前调用筛选器之前。如果你想让你的set_something_to(@account)正常工作,你也应该把它放在before过滤器中。例如:

class ApplicationController < ActionController::Base

  before_filter :configure_account

  protected

    def configure_account
      @account = Account.first
      set_something_to(@account)
    end

end
class ApplicationController
在每次操作之前调用筛选器之前。如果你想让你的set_something_to(@account)正常工作,你也应该把它放在before过滤器中。例如:

class ApplicationController < ActionController::Base

  before_filter :configure_account

  protected

    def configure_account
      @account = Account.first
      set_something_to(@account)
    end

end
class ApplicationController
这给了我一个“未定义的方法”`set\u something\u to'-错误。我通过从我的gem扩展ActionController来添加这个方法。该方法在方法调用之外可用。有什么想法吗?这给了我一个“未定义的方法”“将某物设置为”-错误。我通过从我的gem扩展ActionController来添加这个方法。该方法在方法调用之外可用。有什么想法吗?