从 Rake 任务调用应用程序控制器函数
我想从 rake 任务中调用 ApplicationController 中的函数。我添加了 =>; :environment 标签,但它只是不想工作。
这是我的精简代码 -
lib\taks\autoscrape.rake:
desc "This task will scrape all the movies without info"
task(:autoscrape => :environment) do
require 'application' #probably extraneous
require File.dirname(__FILE__) + '/../../config/environment' #probably extraneous
unless ApplicationController.is_admin?
logger.error = "Sorry, you're not allowed to do that"
return
end
app\controller\application_controller.rb:
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
def is_admin?
session[:is_admin] && session[:is_admin] > 0
end
end
result:
rake scrape:autoscrape --trace
** Invoke scrape:autoscrape (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute scrape:autoscrape
rake aborted!
undefined method `is_admin?' for ApplicationController:Class
E:/Dropbox/My Dropbox/Ruby/moviecat/lib/tasks/autoscrape.rake:11
我的其他控制器一直调用此代码,没有问题。我的 Rake 任务如何调用这段代码?这大大简化了,它是一个更大问题的一部分,我想重用更多代码。
谢谢!!
I want to call a function in my ApplicationController from a rake task. I've added the => :environment tag, but it just doesn't want to work.
Here is my stripped down code-
lib\taks\autoscrape.rake:
desc "This task will scrape all the movies without info"
task(:autoscrape => :environment) do
require 'application' #probably extraneous
require File.dirname(__FILE__) + '/../../config/environment' #probably extraneous
unless ApplicationController.is_admin?
logger.error = "Sorry, you're not allowed to do that"
return
end
app\controller\application_controller.rb:
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
def is_admin?
session[:is_admin] && session[:is_admin] > 0
end
end
result:
rake scrape:autoscrape --trace
** Invoke scrape:autoscrape (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute scrape:autoscrape
rake aborted!
undefined method `is_admin?' for ApplicationController:Class
E:/Dropbox/My Dropbox/Ruby/moviecat/lib/tasks/autoscrape.rake:11
My other controllers call this code all the time, no problems. How can my Rake task call this code? This is greatly simplified, it is part of a bigger problem, i would like to reuse more code.
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的 rake 任务中,您有:
您应该将 .new 添加到控制器名称中:
In your rake task you have:
You should add .new to controller name:
首先,您收到的错误是因为您正在调用
ApplicationController.is_admin?
,但该方法未定义,因为您的方法是在ApplicationController
的实例上定义的,而不是在>ApplicationController
类。其次,会话的概念(至少对我来说)在 rake 任务中并没有多大意义。除了命令行上的用户会话之外,没有真正的会话,这不是您所得到的。
老实说,我不知道从继承自
ApplicationController
或ActionController::Base
的类之外的任何地方调用控制器操作/方法的最佳方法,或者为什么你想要这样做。这些操作/方法是专门设计用于在请求期间使用的,而不是您随时调用的某些操作。如果您确实需要某些东西并且不想重新定义它,请将其放入模型/库中并包含它。First, the error you are getting is because you are calling
ApplicationController.is_admin?
which isn't defined because your method is defined on instances ofApplicationController
, not on theApplicationController
class.Second, the concept of
session
(at least to me) doesn't really make too much sense in a rake task. There are no real sessions other than your user's session at the command line which is not what you would be getting.To be honest, I don't know the best way for going about calling a Controller action/method from anywhere outside of classes that inherit from
ApplicationController
orActionController::Base
, or why you would want to. Those actions/methods are specifically designed to be used during a request, not some action that you call whenever. If you really need something and don't want to redefine it, put it in a model/library and include it.创建控制器实例并调用方法。控制器内的方法应该是公共方法。
例子:
Create a instance for controller and call the method.method inside the controller should be public method.
Example:
也许rajat的理由没有意义,但这个问题是有效的。
我需要做一些维护,并且需要在类中执行一个函数。这是一种一次性解决问题的解决方法(例如迁移),并且不一定要通过正确的方式来完成。
可以通过调用 url 而不是调用 rake 来完成(通过适当的路由和参数设置)。
这可以通过在类外定义函数(使其公开)来完成。
或者也可以按照 William Richerd 的提议来完成,顺便说一下,这确实有效。
Maybe the reason of rajat does not make sense, but the question is valid.
I need to do some maintenance and I need to execute a function inside a class. It is a workaround to get things fixed one time (like migration), and it is not imperative to do it by the proper way.
It can be done calling a url instead of calling a rake (with suitable setting of route and parameters).
It can be done by defining functions out of class (making it public).
Or it can be done as William Richerd has proposed, which, by the way, it works.