我想将ActionView::Helpers::NumberHelper#number_with_precision方法应用于Rake任务中我的(活动记录)模型的对象的属性。我希望这能起作用(但它的错误,完整的跟踪如下):
desc 'test if helpers accessible'
task helper_access: :environment do
include ActionView::Helpers
puts number_with_precision(Purchase.first.payment, strip_insignificant_zeros: true)
end
我似乎能够在Rake任务中包含ActionView::Helpers,以成功引用#number_with_precision,并在Rake任务中 * 单独 * 成功引用我的模型(当 * 不 * 包含ActionView::Helpers时)。但是两者一起做,我得到一个错误。
这是我尝试过的。请帮我一下好吗?
desc 'test if helpers accessible'
task helper_access: :environment do
include ActionView::Helpers
puts number_with_precision(10.0, strip_insignificant_zeros: true)
end
$ rake helper_access # 10
desc 'test if helpers accessible'
task helper_access: :environment do
puts Purchase.first.payment
end
$ rake helper_access # 7000.0
desc 'test if helpers accessible'
task helper_access: :environment do
include ActionView::Helpers
puts Purchase.first.payment
end
E, [2023-05-11T10:51:23.426378 #7159] ERROR -- : Exception rescued in listen-run_thread:
ArgumentError: wrong number of arguments (given 1, expected 2..5)
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/actionview-6.1.7/lib/action_view/helpers/form_options_helper.rb:163:in `select'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:317:in `readpartial'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:290:in `read_events'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:263:in `process'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:236:in `block in run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:232:in `synchronize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rb-inotify-0.10.1/lib/rb-inotify/notifier.rb:232:in `run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/adapter/linux.rb:38:in `_run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/adapter/base.rb:79:in `block in start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/thread.rb:26:in `rescue_and_log'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/thread.rb:18:in `block in new'
--- Thread.new ---
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/adapter/base.rb:75:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/forwardable.rb:238:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/listener.rb:71:in `block in <class:Listener>'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:124:in `instance_eval'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:124:in `call'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:105:in `transition_with_callbacks!'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/fsm.rb:72:in `transition'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/listen-3.7.1/lib/listen/listener.rb:92:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:109:in `start'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:88:in `initialize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:44:in `new'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/evented_file_update_checker.rb:44:in `initialize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application/finisher.rb:233:in `new'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application/finisher.rb:233:in `block in <module:Finisher>'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:32:in `instance_exec'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:32:in `run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:61:in `block in run_initializers'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `call'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/initializable.rb:60:in `run_initializers'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application.rb:391:in `initialize!'
/home/simbed/environment/attendance_system/config/environment.rb:5:in `<main>'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/zeitwerk-2.6.5/lib/zeitwerk/kernel.rb:38:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:332:in `block in require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:299:in `load_dependency'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.7/lib/active_support/dependencies.rb:332:in `require'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application.rb:367:in `require_environment!'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.7/lib/rails/application.rb:533:in `block in run_tasks_blocks'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:83:in `block in run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
/home/simbed/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/home/simbed/.rbenv/versions/3.0.1/bin/rake:23:in `load'
/home/simbed/.rbenv/versions/3.0.1/bin/rake:23:in `<main>'
7000
1条答案
按热度按时间amrnrhlw1#
我不能在Ruby 3.1 + Rails 7.0上重现这个问题。另一种方法是创建一个独立的helper对象,以避免使用可能冲突的方法污染main对象: