0

好的,在网上搜索了将近两个小时但没有找到我的问题的答案后,我会在这里问它。

我有这个方法

类 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

在我的

类匹配 < ActiveRecord::Base

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>

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

4

1 回答 1

0

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

于 2013-09-17T19:45:38.113 回答