Heroku Scheduler:CreditsController的未定义局部变量或方法`params’:Class

我正在尝试使用Heroku Scheduler自动执行任务。 我在本地测试了这个任务并且它有效。 我创建了一个临时路线进行测试,每当我点击路线时它都有效:

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 

然后我创建了一个文件

 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 

当任务执行时,我收到以下错误:

 2014-03-20T21:42:11.190318+00:00 app[scheduler.4496]: undefined local variable or method `params' for CreditsController:Class 

完整日志:

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

===========

更新:我从方法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 

我收到以下错误:

 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  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  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 ' Tasks: TOP => create_expired_credit_lines (See full trace by running task with --trace) 

您应该使用参数定义create_expired_lines方法

 def self.create_expired_lines(args) ... @new_credit = Credit.new(args) ... end 

因为您无法访问此级别的params哈希。

您可以使用适当的参数从RSpec调用此方法。

例如:

 CreditsController.create_expired_lines(attr1: value, attr2: value)