Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在ActionController::Base之外的类中正确包含ActionView::Helpers?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在ActionController::Base之外的类中正确包含ActionView::Helpers?

Ruby on rails 如何在ActionController::Base之外的类中正确包含ActionView::Helpers?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想创建自定义类,该类将为用户生成通知。我的问题是:如何通过link\u toRails助手在通知文本中生成指向资源的链接 我一直在尝试: 遵循“如何在模型中使用链接到”的说明和类似答案:包括ActionView::Helpers::UrlHelper和Rails.application.routes.url\u Helpers进入Maker类,并出现以下错误,当我尝试使用link\u to时,出现在create\u text方法中: EmployeesControllerTest#test_c

我想创建自定义类,该类将为用户生成通知。我的问题是:如何通过
link\u to
Rails助手在通知文本中生成指向资源的链接

我一直在尝试:

遵循“如何在模型中使用
链接到
”的说明和类似答案:包括
ActionView::Helpers::UrlHelper
Rails.application.routes.url\u Helpers
进入
Maker
类,并出现以下错误,当我尝试使用
link\u to
时,出现在
create\u text
方法中:

EmployeesControllerTest#test_company_vacancy_daily:
NameError: undefined local variable or method `controller' for #<Notifies::Maker:0x000000069ae218>
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionview-5.0.0.1/lib/action_view/routing_url_for.rb:132:in `optimize_routes_generation?'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:192:in `optimize_routes_generation?'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:172:in `call'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:295:in `block (2 levels) in define_url_helper'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/polymorphic_routes.rb:262:in `handle_model_call'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionview-5.0.0.1/lib/action_view/routing_url_for.rb:116:in `url_for'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionview-5.0.0.1/lib/action_view/helpers/url_helper.rb:196:in `link_to'
    /app/lib/notifies.rb:32:in `block in create_text'
    /app/lib/notifies.rb:32:in `map!'
    /app/lib/notifies.rb:32:in `create_text'
    /app/lib/notifies.rb:38:in `run'
    /app/test/controllers/notifications_test.rb:167:in `block (2 levels) in <class:EmployeesControllerTest>'
    /usr/local/rvm/gems/ruby-2.2.5/gems/activesupport-5.0.0.1/lib/active_support/testing/assertions.rb:71:in `assert_difference'
    /app/test/controllers/notifications_test.rb:166:in `block in <class:EmployeesControllerTest>'

我建议使用Presenter模式的变体(它允许在普通的旧Ruby对象中调用像
link\u to
这样的方法)。Ryan Bates对这个话题有很好的理解。下面是我大致的做法

首先,我要创建一个
PresenterBase
类(我在
app
目录中保留一个
presenters
文件夹):

最后,您的控制器可能如下所示:

  class Notifies::Maker < PresenterBase

    class << self
      def run(controller) new(controller).run end
    end

      def run
        to_notify.notify(kind: kind, text: text) if popularities.any?
      end

    private

      def kind()            @kind ||= get_controller_variable(:kind)      end
      def model()           @model ||= get_controller_variable(:model)    end
      def past_time()       present_time - 24.hours                       end
      def popularities()    @popularities ||= get_popularities            end
      def present_time()    @present_time ||= Time.now.utc                end

      def to_notify() 
        case model.class.to_s
        when 'Company'
          model.admin
        when 'Employee'
          model
        end
      end

      def get_popularities
        Popularity.where(to: model.entity.id).select do |e|
          e.updated_at.utc.between?(past_time, present_time)
        end
      end

      def text
        'Those users interested in your contacts: ' << links
      end

      def links
        popularities.
          map!{ |p| p.from_entity.turn }.
          map!{ |p| link_to p.full_name, p }.
          join(', ')
      end

  end
  class FooController < ApplicationController

    def foo_method
      @model = some_method_to_set_model
      @kind = some_method_to_set_kind
      Notifies::Maker.run(self)
    end

  end
class FooController

我没有测试,所以它可能有问题。但是,也许这会有帮助。

我知道怎么做了。错误是由于调用的
url\u不正确造成的,应该使用参数
only\u path调用它,如下所示:

def create_text(popularities=build_list)
text='那些对您的联系人感兴趣的用户:'
大众地图!做接受者|
链接到receiver.full\u name,url\u([receiver,only\u path:true])
结束
text+popularity.join(',')
结束

或者在enviropment配置文件中设置一个
默认\u主机
参数

谢谢,伙计,但这似乎太复杂了,不能只生成一行带链接的文本,所以我做了更多的研究,找到了答案,发布在下面。
  class FooController < ApplicationController

    def foo_method
      @model = some_method_to_set_model
      @kind = some_method_to_set_kind
      Notifies::Maker.run(self)
    end

  end