gpt4 book ai didi

ruby-on-rails - Rails错误[:base] inside class method?

转载 作者:行者123 更新时间:2023-12-03 08:57:43 26 4
gpt4 key购买 nike

好的,经过将近两个小时的搜索,没有找到我的问题的答案,我将在这里提出问题。

我有这种方法

类MatchesController

def import
if Match.find_by_match_id(params[:match_id])
redirect_to matches_url, notice: "Match already imported."
else
Match.import(params[:match_id], params[:league_id])
# error handling here?
redirect_to matches_url, :flash => { :success => "Match imported successfully." }
end
end

在我的

类Match

def self.import(match,league)
begin
transaction do
uri = URI('https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/')
params = { :match_id => match,
:key => "MY KEY" }

uri.query = URI.encode_www_form(params)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
resp = http.request(request)

data = resp.body

result = JSON.parse(data)

if result['result']['error']
errors[:base] << "No Match with given ID found"
#self.errors.add_to_base("No Match with given ID found")
break
end

match = result['result']
players = match['players']
pickban = match['picks_bans']
leagueid = match['leagueid']

if league != leagueid
errors.add_to_base("This Match doesn't belong to the selected League")
break
end
end
rescue ActiveRecord::RecordInvalid => invalid
# not yet implemented
end
end

现在,当我导入一个匹配项但找不到该匹配项时,我想添加一个可以显示给用户的错误,但是出现此错误
undefined local variable or method `errors' for #<Class:0x007fde27cba478>

我在这里做错了什么?以及如何通过 flash向用户显示错误?

最佳答案

#errors是一个实例方法,您可以在Match类上调用它。您可以编写import方法以使其具有“匹配成功导入”或错误消息的返回值。

关于ruby-on-rails - Rails错误[:base] inside class method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18858479/

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