gpt4 book ai didi

ruby-on-rails - Rails - NoMethodError

转载 作者:行者123 更新时间:2023-12-02 02:02:18 26 4
gpt4 key购买 nike

当我的 DeltaSynWorker 运行时,我收到 NoMethodErrors。这发生在作为当前工作应用程序的修订版构建的应用程序中。我不是最初的程序员,我是从 Java 背景开始的(我提到这一点是因为我可能遗漏了一些对其他人来说非常明显的东西)。当代码与当前在不同 Web 应用程序中正常运行的代码非常相似时,我无法弄清楚为什么会抛出 NoMethodeError

错误:

NoMethodError: undefined method `client' for #<ApiRequestBuilder:0x0000000705a8f0>

delta_sync_worker.rb

class DeltaSyncWorker < SyncWorker

include Sidekiq::Worker
sidekiq_options queue: "delta_syncs"

def perform(subscriber_id, client_id)

sleep(10)
current_subscriber = ApiSubscriberDecorator.decorate(Subscriber.find(subscriber_id))
Time.zone = current_subscriber.time_zone
client = ApiClientDecorator.decorate(Client.find(client_id))
arb = ApiRequestBuilder.new(URI.parse(SR_HOST + '/servlet/sync/process'))

if arb.client(:subscriber => current_subscriber, :client => client)

arb.transmit

if arb.success?
current_subscriber.touch(:sync_updated_at)
decorated_client = ClientDecorator.decorate(client.model)
ConfirmationsSyncWorker.perform_in(1.hours, current_subscriber.id)
else
error_params = {:subscriber => current_subscriber.id, :response_body => arb.response.body, :request_body => arb.request.body, :sync_type => "DeltaSyncWorker"}
Airbrake.notify(:error_class => "sync_error", :error_message => "Sync Error: #{arb.response.message}", :params => error_params)
end
end
end
end

api_request_builder.rb

require 'nokogiri'
class ApiRequestBuilder < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths

self.view_paths = "app/api"

attr_accessor :request_body, :request, :response, :append_request_headers, :request_method, :url

def initialize(url, *args)
@url = url
if args
args.each do |arg|
arg.each_pair{ |k,v| instance_variable_set("@#{k.to_s}", v) }
end
end
end

# this will search for an api request template in api/requests, render that template and set any instance variables
def method_missing(meth, *args, &block)

if lookup_context.template_exists?("requests/#{meth.to_s}")

if args
args.each do |arg|
arg.each_pair{|k,v| instance_variable_set("@#{k.to_s}", v) }
end
end

@request_body = (render :template => "requests/#{meth.to_s}")
else
super
end

end

def transmit
@request_method ||= "Post"
@request = "Net::HTTP::#{@request_method}".constantize.new(@url.path)
@request['x-ss-user'] = @subscriber.sr_user if @subscriber && @subscriber.sr_user.present?
@request['x-ss-pwd'] = @subscriber.sr_password if @subscriber && @subscriber.sr_password.present?

unless @append_request_headers.nil?
@append_request_headers.each_pair{ |k,v| @request[k] = v }
end
@request.body = @request_body if request_body? && @request.request_body_permitted?

@http = Net::HTTP.new(@url.host, @url.port)
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@response = @http.request(@request)

end

def success?
if @response.code == 200.to_s
return true
else
return false
end
end

def request_body?
unless @request_body.nil?
return true
else
return false
end
end
end

我一直在查看其他 NoMethodError 问题,但找不到适合我情况的答案。任何帮助是极大的赞赏。谢谢!

最佳答案

method_missing 将捕获未定义方法的已发送消息,最后调用 super 会将其传递给 Ruby 的标准 method_missing 行为,这就是您所看到的 (NoMethodError)。但是,只有当 if 条件不满足时才会发生这种情况,我怀疑这是这里发生的情况,并且可以解释为什么它在某些情况下有效但在其他情况下无效。对 :client 的调用在继承链中没有找到匹配的方法,将查找名为“requests/client”的模板 - 尝试添加此模板并查看是否能解决问题。

关于ruby-on-rails - Rails - NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16699273/

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