好的,经过近两个小时的网络搜索而没有找到我的问题的答案,我会在这里问一下。
我的
中有这个方法类MatchesController< ApplicationController中
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
在我的
中类匹配<的ActiveRecord ::基
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
向用户显示错误?
答案 0 :(得分:0)
#errors
是一个实例方法,您在Match
类上调用它。您可以编写import
方法,使其返回值为“匹配导入成功”或错误消息。