ruby-on-rails – 如何在resque作业中使用rails helpers?
发布时间:2020-12-16 23:33:12 所属栏目:百科 来源:网络整理
导读:我正在尝试在我的resque工作中使用一些帮助器并遇到问题.这是我尝试过的: require 'json'class SoulmateUserFollowing tried this - include Rails.application.routes.url_helpers and this - include ActionView::Helpers:UrlHelper and this - helper Ac
我正在尝试在我的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 我还需要使用image_path方法包含帮助程序,并在模块ImageHelper中包含我的自定义帮助程序. 解决方法
在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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |