开发者

How to access routes in Resque worker class?

开发者 https://www.devze.com 2023-03-27 09:35 出处:网络
I am running on Rails 3.1 RC5. Resque doesn\'t seem like it is ab开发者_StackOverflow社区le to access root_url from a worker class. I tried putting include Rails.application.routes.url_helpers to no a

I am running on Rails 3.1 RC5. Resque doesn't seem like it is ab开发者_StackOverflow社区le to access root_url from a worker class. I tried putting include Rails.application.routes.url_helpers to no avail.

For example:

module EncodePhoto
  include Rails.application.routes.url_helpers

  def self.perform()
    puts root_url
  end
end

Returns:

undefined method `root_url' for EncodePhoto:Module


If I am in a rails console, I can write:

include ActionController::UrlWriter

and then I can access the routes I need. Perhaps it can apply to your problem as well?

EDIT: Please see this. Here's the accepted answer from there:

include ActionDispatch::Routing::UrlFor
include ActionController::PolymorphicRoutes
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'
0

精彩评论

暂无评论...
验证码 换一张
取 消