2013-01-14 81 views
2

我在"Rails Root"/lib/tasks/example.rake下面的代码中定义一个定制类中:Rails和Rake任务 - 如何调用一个Rails方法耙子任务文件

task :example_task => :environment do 
    e = Example.new 
    e.example_method 
end 

class Example 
    def example_method 
    select_tag 'Example' 
    end 
end 

当我在rake任务调用e.example_method,我得到错误"undefined method 'select_tag' for #<Example:0x39f58b0>"

select_tag是Rails方法:http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag

如何让select_tag电话的工作?

+2

你只需要要求您需要的帮手。请检查这个[问题] [1]。 [1]:http://stackoverflow.com/questions/1450112/how-do-i-use-helpers-in-rake –

+1

'select_tag'是在'ActionView'为什么助手方法之一您的Rake Task会调用与视图相关的任何方法吗?只是好奇 –

回答

0

你需要帮助者你正在尝试使用的方法。在select_tag的情况下,您需要要求ActionView

0

您可以:

通过辅助变量访问的辅助方法:

helper.content_tag :li, "Helola" 
=> "<li>Helola</li>" 

或要求所需的帮手

require "#{RAILS_ROOT}/app/helpers/some_helper.rb" 
include SomeHelper 
相关问题