gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:07:27 25 4
gpt4 key购买 nike

我想创建将为用户生成通知的自定义类。我的问题是:如何通过 link_to Rails helper 在通知文本中生成指向资源的链接?

我已经尝试过的:

遵循 this 中的说明和类似的答案,如“如何在模型中使用 link_to”:包括 ActionView::Helpers::UrlHelperRails.application.routes.url_helpers进入 Maker 类并出现以下错误,当我尝试使用 link_to 时出现在 create_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>'

我的代码是:

module Notifies
class Maker

include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers

def initialize(model, kind)
@model = model
@kind = kind

case @model.class.to_s
when 'Company'
@to = @model.admin
when 'Employee'
@to = @model
end
end

def build_list
present = Time.now.utc
past = present - 24.hours

popularities = Popularity.where(to: @model.entity.id).select do |e|
e.updated_at.utc.between?(past, present)
end

popularities.map! { |p| p.from_entity.turn }
end

def create_text(popularities = build_list)
text = 'Those users interested in your contacts: '
popularities.map! { |p| link_to p.full_name, p }
text + popularities.join(', ')
end

def run
popularities = build_list
text = create_text(popularities)
@to.notify(kind: @kind, text: text) if popularities.any?
end
end
end

最佳答案

我想出了如何做到这一点。该错误是由错误的url_for 调用引起的,应该使用参数only_path 调用它,如下所示:

def create_text(popularities = build_list)
text = 'Those users interested in your contacts: '
popularities.map! do |reciever|
link_to reciever.full_name, url_for([reciever, only_path: true])
end
text + popularities.join(', ')
end

或者作为替代方案,应在环境配置文件中设置一个default_host 参数。

关于ruby-on-rails - 如何在 ActionController::Base 之外的类中正确包含 ActionView::Helpers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com