我正在尝试在我的应用中设置Zendesk API,因此我决定使用API that was built by Zendesk

我已经设置了初始化程序对象来加载客户端。

config/initializers/zendesk.rb

require 'zendesk_api'

client = ZendeskAPI::Client.new do |config|
  # Mandatory:
  config.url = Rails.application.secrets[:zendesk][:url]

  # Basic / Token Authentication
  config.username = Rails.application.secrets[:zendesk][:username]
  config.token = Rails.application.secrets[:zendesk][:token]

  # Optional:

  # Retry uses middleware to notify the user
  # when hitting the rate limit, sleep automatically,
  # then retry the request.
  config.retry = true

  # Logger prints to STDERR by default, to e.g. print to stdout:
  require 'logger'
  config.logger = Logger.new(STDOUT)

  # Changes Faraday adapter
  # config.adapter = :patron

  # Merged with the default client options hash
  # config.client_options = { :ssl => false }

  # When getting the error 'hostname does not match the server certificate'
  # use the API at https://yoursubdomain.zendesk.com/api/v2
end


这几乎是网站上的复制粘贴,但是我决定使用token + username组合。

然后,我创建了一个服务对象,该对象传递一个JSON对象并使其构造票证。从控制器调用该服务对象。

app/services/zendesk_notifier.rb

class ZendeskNotifier
  attr_reader :data

  def initialize(data)
    @data = data
  end

  def create_ticket
    options = {:comment => { :value => data[:reasons] }, :priority => "urgent" }
    if for_operations?
      options[:subject] = "Ops to get additional info for CC"
      options[:requester] = { :email => '[email protected]' }
    elsif school_in_usa_or_canada?
      options[:subject] = "SRM to communicate with student"
      options[:requester] = { :email => '[email protected]' }
    else
      options[:subject] = "SRM to communicate with student"
      options[:requester] = { :email => '[email protected]' }
    end
    ZendeskAPI::Ticket.create!(client, options)
  end

  private
  def for_operations?
    data[:delegate] == 1
  end

  def school_in_usa_or_canada?
    data[:campus_country] == "US" || "CA"
  end
end


但是现在我越来越

NameError - undefined local variable or method `client' for #<ZendeskNotifier:0x007fdc7e5882b8>:
  app/services/zendesk_notifier.rb:20:in `create_ticket'
  app/controllers/review_queue_applications_controller.rb:46:in `post_review'


我以为客户端与我的配置初始化程序中定义的客户端相同。我以某种方式认为现在这是一个不同的对象。我试图查看他们的文档以获取更多信息,但是我对这是什么一无所知?

最佳答案

如果要使用初始化程序中定义的client,则需要通过将其更改为$client使其全局。当前,您已将其设置为局部变量。

10-07 17:29