我正在尝试在我的resque工作中使用一些帮助器并遇到问题.这是我尝试过的:
- require 'json'
- class SoulmateUserFollowing
- tried this -> include Rails.application.routes.url_helpers
- and this -> include ActionView::Helpers:UrlHelper
- and this -> helper ActionView::Helpers::UrlHelper
- @queue = :soulmate_user
- def self.perform(user_id)
- user = User.find(user_id)
- url = url_for(following_user)
- end
- end
解决方法
在config / routes.rb文件中添加命名路由,然后从您的作业类中调用它(不需要包含任何内容)
- Rails.application.routes.url_helpers.following_user_url(following_user)
您还必须在您的环境中设置默认主机,因为您在“resque”内部并且没有设置http参数.
- routes.default_url_options = {:host => "somehost.com"}
或者,您可以在您的课程中包含url_helpers并执行类似的操作
- class SoulmateUserFollowing
- include Rails.application.routes.url_helpers
- @queue = :soulmate_user
- def initialize(user_id)
- user = User.find(user_id)
- url = url_for(following_user)
- end
- def self.perform(user_id)
- new(user_id)
- end
- end