2

I have the following code in "Rails Root"/lib/tasks/example.rake:

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

class Example
  def example_method
    select_tag 'Example'
  end
end

When I call e.example_method in the rake task, I get the error "undefined method 'select_tag' for #<Example:0x39f58b0>".

select_tag is a Rails method: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag

How do I make the select_tag call work?

Miscreant
  • 6,626
  • 3
  • 22
  • 21
  • 2
    You just need to require the helpers you need. Please check this [question][1]. [1]: http://stackoverflow.com/questions/1450112/how-do-i-use-helpers-in-rake – Alexandre Abreu Jan 14 '13 at 16:43
  • 1
    `select_tag` is one of the helpers methods in `ActionView` Why would your Rake Task calling any method that is view related? Just curious – Jasdeep Singh Jan 14 '13 at 17:08

2 Answers2

0

You need to require the helper the method you're trying to use. In the case of select_tag, you'll need to require ActionView.

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
0

You can either:

Access helper methods via the helper variable:

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

or require the desired helpers

require "#{RAILS_ROOT}/app/helpers/some_helper.rb"
include SomeHelper
ichigolas
  • 7,595
  • 27
  • 50