Heroku Scheduler:Credits定义的LocalController:Class的未定义局部变量或方法`params' [英] Heroku Scheduler: undefined local variable or method `params' for CreditsController:Class
问题描述
credits.controller.rb
class CreditsController< ApplicationController
...
def self.create_expired_lines
...
@new_credit = Credit.new(params [:credit])
.. 。
end
然后我创建了一个文件
app / lib / tasks / scheduler.rake
desc此任务由Heroku调度程序加载项
任务:create_expired_credit_lines => :环境做
放置创建过期的信用额度......
CreditsController.create_expired_lines
放置完成。
end
执行任务时,出现以下错误:
2014-03-20T21:42:11.190318 + 00:00 app [scheduler.4496]:Credits定义的未定义局部变量或方法`params' :Class
完整日志:
2014-03-20T21:42:06.228208 + 00:00 heroku [scheduler.4496]:用命令`bundle exec rake create_expired_credit_lines`启动进程
2014-03-20T21:42: 08.725450 + 00:00 heroku [scheduler.4496]:状态从开始改为
2014-03-20T21:42:09.410565 + 00:00 app [scheduler.4496]:弃权警告:您的Rails 2.3-风格的插件在供应商/插件!在Rails 4.0中将会删除对这些插件的支持。将它们移出并将它们捆绑到Gemfile中,或者将它们作为lib / myplugin / *和config / initializers / myplugin.rb折叠到您的应用程序中。有关更多信息,请参阅发行说明:http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released。 (从/ app / Rakefile中的< top(required)>调用:7)
2014-03-20T21:42:09.410565 + 00:00 app [scheduler.4496]:DEPRECATION警告:您有Rails 2.3供应商/插件中的插件!在Rails 4.0中将会删除对这些插件的支持。将它们移出并将它们捆绑到Gemfile中,或者将它们作为lib / myplugin / *和config / initializers / myplugin.rb折叠到您的应用程序中。有关更多信息,请参阅发行说明:http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released。 (从/ app / Rakefile中的< top(required)>调用:7)
2014-03-20T21:42:10.274625 + 00:00 app [scheduler.4496]:连接到由DATABASE_URL指定的数据库
2014-03-20T21:42:10.563939 + 00:00 app [scheduler.4496]:[已弃用] I18n.enforce_available_locales将在未来默认为true。如果你真的想跳过你的语言环境验证,你可以设置I18n.enforce_available_locales = false来避免这个消息。
2014-03-20T21:42:10.852664 + 00:00 app [scheduler.4496]:创建过期的信用额度...
2014-03-20T21:42:11.190030 + 00:00 app [ scheduler.4496]:rake中止!
2014-03-20T21:42:11.191365 + 00:00 app [scheduler.4496]:/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation /delegation.rb:6:in`each'
2014-03-20T21:42:11.191365 + 00:00 app [scheduler.4496]:/app/vendor/bundle/ruby/1.9.1/gems/ activerecord-3.2.11 / lib / active_record / relation / delegation.rb:6:在`each'
2014-03-20T21:42:11.191365 + 00:00 app [scheduler.4496]:/ app / app /controllers/credits_controller.rb:147:in`create_expired_lines'
2014-03-20T21:42:11.191569 + 00:00 app [scheduler.4496] :(通过使用--trace运行任务查看完整跟踪)
2014-03-20T21:42:11.191365 + 00:00 app [scheduler.4496]:/app/app/controllers/credits_controller.rb:158:in`block in create_expired_lines'
2014-03- 20T21:42:11.190318 + 00:00 app [scheduler.4496]:Credits定义的未定义局部变量或方法`params':Class
2014-03-20T21:42:11.191478 + 00:00 app [scheduler.4496 ]:任务:TOP => create_expired_credit_lines
2014-03-20T21:42:11.191365 + 00:00 app [scheduler.4496]:/app/lib/tasks/scheduler.rake:4:in`block in< top(required)> '
2014-03-20T21:42:12.567208 + 00:00 heroku [scheduler.4496]:进程退出状态1
2014-03-20T21:42:12.576344 + 00:00 heroku [scheduler .4496]:状态从最新改为完成
===========
UPDATE:
我从Credit.new(params [:credit])方法中删除了参数,它现在看起来像这样:
credits.controller.rb
class CreditsController< ApplicationController
...
def new
...
@credit = Credit.new
@ credit.user_id = params [:id]
。 ..
end
def self.create_expired_lines
...
@new_credit = Credit.new(params [:credit])
...
end
当我运行命令时
$ heroku run rake create_expired_credit_lines
我得到以下错误:
运行`rake create_expired_credit_lines`附加到终端... up,run.3757
拒绝警告:您在供应商/插件中使用Rails 2.3风格的插件!在Rails 4.0中将会删除对这些插件的支持。将它们移出并将它们捆绑到Gemfile中,或者将它们作为lib / myplugin / *和config / initializers / myplugin.rb折叠到您的应用程序中。有关更多信息,请参阅发行说明:http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released。 (从/ app / Rakefile中的< top(required)>调用:7)
DEPRECATION WARNING:在供应商/插件中有Rails 2.3风格的插件!在Rails 4.0中将会删除对这些插件的支持。将它们移出并将它们捆绑到Gemfile中,或者将它们作为lib / myplugin / *和config / initializers / myplugin.rb折叠到您的应用程序中。有关更多信息,请参阅发行说明:http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released。 (从/ app / Rakefile中的< top(required)>调用:7)
连接到由DATABASE_URL指定的数据库
[不建议] I18n.enforce_available_locales将在默认情况下为true。如果你真的想跳过你的语言环境验证,你可以设置I18n.enforce_available_locales = false来避免这个消息。
创建过期的信用额度...
耙子中止!
对于CreditsController:
/app/app/controllers/credits_controller.rb:177:in`block in create_expired_lines'$ b $ app / vendor / bundle / ruby / 1.9.1 / gems / activerecord- 3.2.11 / lib / active_record / relation / delegation.rb:6:在'each'中
/app/vendor/bundle/ruby/1.9.1/gems/activerecord -3.2.11 / lib / active_record / relation / delegation.rb:6:在`each'
/app/app/controllers/credits_controller.rb:154:in`create_expired_lines'
/ app / lib /tasks/scheduler.rake:4:in`block in< top(required)>'
Tasks:TOP => create_expired_credit_lines
(通过使用-trace运行任务来查看完整跟踪)
您应该使用参数定义 create_expired_lines
方法
def self.create_expired_lines(args)
...
@new_credit = Credit.new(args)
...
end
因为您无法在此级别访问参数哈希。
可以使用适当的参数从RSpec调用此方法。
例如:
CreditsController.create_expired_lines(attr1:value,attr2:value)
I am trying to automate a task with Heroku Scheduler. I tested the task in local and it works. I created a temporary route for testing, and it works everytime I hit the route:
get 'create_expired_credit_lines' to: 'credits#create_expired_lines'
credits.controller.rb
class CreditsController < ApplicationController ... def self.create_expired_lines ... @new_credit = Credit.new(params[:credit]) ... end
I then created a file
app/lib/tasks/scheduler.rake desc "This task is called by the Heroku scheduler add-on" task :create_expired_credit_lines => :environment do puts "Creating expired credit lines..." CreditsController.create_expired_lines puts "done." end
When the tasks get executed, I get the following error:
2014-03-20T21:42:11.190318+00:00 app[scheduler.4496]: undefined local variable or method `params' for CreditsController:Class
Full log:
2014-03-20T21:42:06.228208+00:00 heroku[scheduler.4496]: Starting process with command `bundle exec rake create_expired_credit_lines` 2014-03-20T21:42:08.725450+00:00 heroku[scheduler.4496]: State changed from starting to up 2014-03-20T21:42:09.410565+00:00 app[scheduler.4496]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7) 2014-03-20T21:42:09.410565+00:00 app[scheduler.4496]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7) 2014-03-20T21:42:10.274625+00:00 app[scheduler.4496]: Connecting to database specified by DATABASE_URL 2014-03-20T21:42:10.563939+00:00 app[scheduler.4496]: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. 2014-03-20T21:42:10.852664+00:00 app[scheduler.4496]: Creating expired credit lines... 2014-03-20T21:42:11.190030+00:00 app[scheduler.4496]: rake aborted! 2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:6:in `each' 2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:6:in `each' 2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/app/controllers/credits_controller.rb:147:in `create_expired_lines' 2014-03-20T21:42:11.191569+00:00 app[scheduler.4496]: (See full trace by running task with --trace) 2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/app/controllers/credits_controller.rb:158:in `block in create_expired_lines' 2014-03-20T21:42:11.190318+00:00 app[scheduler.4496]: undefined local variable or method `params' for CreditsController:Class 2014-03-20T21:42:11.191478+00:00 app[scheduler.4496]: Tasks: TOP => create_expired_credit_lines 2014-03-20T21:42:11.191365+00:00 app[scheduler.4496]: /app/lib/tasks/scheduler.rake:4:in `block in <top (required)>' 2014-03-20T21:42:12.567208+00:00 heroku[scheduler.4496]: Process exited with status 1 2014-03-20T21:42:12.576344+00:00 heroku[scheduler.4496]: State changed from up to complete
===========
UPDATE: I removed the arguments from the method Credit.new(params[:credit]) and it now looks like this:
credits.controller.rb
class CreditsController < ApplicationController ... def new ... @credit = Credit.new @credit.user_id = params[:id] ... end def self.create_expired_lines ... @new_credit = Credit.new(params[:credit]) ... end
When I run the command
$ heroku run rake create_expired_credit_lines
I get the following errors:
Running `rake create_expired_credit_lines` attached to terminal... up, run.3757 DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7) Connecting to database specified by DATABASE_URL [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. Creating expired credit lines... rake aborted! undefined local variable or method `params' for CreditsController:Class /app/app/controllers/credits_controller.rb:177:in `block in create_expired_lines' /app/vendor/bundle/ruby/1.9.1/gems/activerecord- 3.2.11/lib/active_record/relation/delegation.rb:6:in `each' /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:6:in `each' /app/app/controllers/credits_controller.rb:154:in `create_expired_lines' /app/lib/tasks/scheduler.rake:4:in `block in <top (required)>' Tasks: TOP => create_expired_credit_lines (See full trace by running task with --trace)
解决方案You should define the
create_expired_lines
method with argumentsdef self.create_expired_lines(args) ... @new_credit = Credit.new(args) ... end
As you don't have access to params hash at this level.
And you can call this method from RSpec with proper arguments.
For eg:
CreditsController.create_expired_lines(attr1: value, attr2: value)
这篇关于Heroku Scheduler:Credits定义的LocalController:Class的未定义局部变量或方法`params'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!