How can I use rail assistants in resque work?

I try to use some helpers in my resque work and run into problems. Here is what I tried:

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 

I also need to enable a helper with the image_path method and a custom helper located in the ImageHelper module.

+6
source share
1 answer

Add the named route to config / routes.rb and then call it from your job class (no need to include anything)

 Rails.application.routes.url_helpers.following_user_url(following_user) 

You must also set the default host in your environment, since you are inside a "resque" and there are no http parameters set.

 routes.default_url_options = {:host => "somehost.com"} 

Alternatively, you can include url_helpers and do something like this in your class

 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 
+8
source

Source: https://habr.com/ru/post/899079/


All Articles