ruby rails 上的 Web 服务在升级到 rails 后无法正常工作 4.x

ruby on rails web service not working after upgrade to rails 4.x

前段时间, 我已经使用 RoR v.3.2.13 编写了一个网络应用程序...... 所有这些都经过测试并正常工作了一段时间...... 现在,我要将所有环境更新到 rails 4.2.5! 我在升级框架时发现了很多问题,在网上找了很多建议都几乎很容易地解决了!!

最后一个给我添麻烦了...

嗯, 我需要将 .xml 流从掌上设备上传到服务器以更新一些数据库记录....

这是移动终端向服务器发送的.xml流的示例:

<?xml version="1.0" encoding="utf-8"?>
<readings>
 <reading>
  <opercode>a001a</opercode>
  <barcode>000073680</barcode>
  <opid>4</opid>
  <datetime>2012-01-22T00:07:34+01:00</datetime>
 </reading>
 <reading>
  <opercode>a001a</opercode>
  <barcode>000073679</barcode>
  <opid>4</opid>
  <datetime>2012-01-22T00:07:38+01:00</datetime>
 </reading>
</readings>

如您所见,它包含大量读数,每个读数包含四个字段。

以防无线连接无法正常工作,我构建了一个包含 html 表单和文件上传字段的视图....

控制器使用与您在此处看到的相同的操作接收流:

.........................
#
# receives the readings stream and passes it to the parser
def upload
  #
  # different part of code if request is from .html o .xml
  respond_to do |format|
    # the request is from .html page
    format.html do
      # checks the uploaded file 'content type'
      if params[:readings_file].instance_variable_get(:@content_type) == "text/xml"
        # the file is .xml
        # identifies the readings operator_id
        operator_id = Operator.find_by_code(params[:readings_file].instance_variable_get(:@original_filename)[0..4]).id
        # retrieves the number of readings stored into .xml file
        original_filename = params[:readings_file].instance_variable_get(:@original_filename).partition(".")[0]
        expected_readings = original_filename.partition("-P-")[2].to_i != 0 ? original_filename.partition("-P-")[2].to_i : original_filename.partition("--")[2].to_i
        # binds the temporary file for nokogiri parsing
        temp_file = File.open(params[:readings_file].instance_variable_get(:@tempfile))
        readings_to_be_loaded = Nokogiri::XML(temp_file)
        # parses the readings records
        result = parse_xml(readings_to_be_loaded, operator_id)
        # checks parsing result against expected readings count
        if result != expected_readings
          message = "WARNING - Inside .xml " + expected_readings.to_s + " records was present, only " + result.to_s + " has been accepted as valid readings!!"
        else
          message = "OK - .xml stream succesfully parsed!"
        end
      else
        # the file is not .xml
        message = "ERROR - Invalid readings file format detected --> 'not xml'!!"
      end
      logger.debug message
      redirect_to readings_path, :flash => {:message => message} and return
    end
    #
    # the request is from .xml stream
    format.xml  do
      if params[:readings].present?
        # determines the number of expected readings inside the stream and retrieves the operator code
        if params[:readings][:reading][0].nil?
          expected_readings = 1
          oper_code = params[:readings][:reading][:opercode]
        else
          expected_readings = params[:readings][:reading].count
          oper_code = params[:readings][:reading][0][:opercode]
        end
        # initializes the good readings counter
        readings_count = 0
        ........

        omissis

        ........
      else
        # the stream don't contains readings, return the http error code 406 -> 'Not Acceptable'
        logger.debug "ERROR - Readings file content is incorrectly formatted!!"
        render nothing: true, status: 406 and return
      end
    end
  end
  # not .xml request nor html upload
  # nothing has been processed, ingores the upload and returns status code 404 -> 'resource not found'
  logger.debug "ERROR - Incorrect data stream content"
  render :nothing => true, status: 404 and return
end
................

然后区分数据流是从 html 作为文件上传还是直接作为 .xml 流上传的操作。

现在, 如果我使用 html 视图上传文件,一切正常(rails 3.2 或 4.2 都可以正常工作并解析读数)...

为了在终端上使用控制台模拟直接 .xml 上传操作,我使用此命令行:

curl -v -H "Content-Type: application/xml; charset=utf-8" --data-ascii @a001a-12115-81843-P-5.xml http://localhost:3000/readings/upload.xml

如果流直接使用.xml 格式上传...rails 3.2 运行正常,rails 4.2 returns “406 不可接受”(见提取日志行!)

日志来自 rails 3.2:

Started POST "/readings/upload.xml" for 127.0.0.1 at 2015-01-22 21:32:35 +0100
Processing by ReadingsController#upload as XML
  Parameters: {"readings"=>{"reading"=>[{"opercode"=>"a001a", "barcode"=>"000073685", "opid"=>"4", "datetime"=>"2015-01-21T20:18:20+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073683", "opid"=>"4", "datetime"=>"2015-01-21T20:18:24+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073682", "opid"=>"4", "datetime"=>"2015-01-21T20:18:28+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073679", "opid"=>"4", "datetime"=>"2015-01-21T20:18:36+01:00"}, {"opercode"=>"a001a", "barcode"=>"000073683", "opid"=>"4", "datetime"=>"2015-01-21T20:18:41+01:00"}]}}
  Rendered text template (0.0ms)
Completed 200 OK in 49ms (Views: 48.2ms | ActiveRecord: 0.0ms)

日志来自 rails 4.2:

Started POST "/readings/upload.xml" for 127.0.0.1 at 2016-05-27 21:11:38 +0200
Processing by ReadingsController#upload as XML
ERROR - Readings file content is incorrectly formatted!!
  Rendered text template (0.0ms)
Completed 406 Not Acceptable in 2ms (Views: 0.2ms | ActiveRecord: 0.0ms)

似乎参数根本没有达到控制器的动作!! (我想这是由于对 4.2 版框架的某种限制,但我无法找到任何具体信息来证实或反驳我的怀疑)

请注意: 如果我尝试使用掌上设备直接发送流,我会收到相同的响应!!

任何建议都被接受,

非常感谢大家... 弗朗切斯科

我已经解决了...

测试再测试.... 这不是问题!!

我发现当您在开发环境中启动 rails 时.... 它只监听本地主机地址,所以如果你需要测试应用程序与外部设备的交互,你需要明确地让它监听你的开发电脑 public ip 地址...

这是按照此处所述完成的:

现在数据到达网络服务并且 xml 流被正确处理!