Post/Put 请求的路由错误(Passenger Headers) [英] Routing Error with Post/Put requests (Passenger Headers)

查看:47
本文介绍了Post/Put 请求的路由错误(Passenger Headers)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,经过大量研究后无法解决.我有几种通过 Carrierwave 上传文件的表格.当我上传信息时,部分路线被切断(我认为).

I've run into a weird problem and after a bunch of research can't get any closer. I've got several forms that upload files via Carrierwave. When I upload the information, part of the route gets cut off (I think).

例如,我有一个多部分表单提交给:

For example, I have a multi-part form submitting to:

https:/domain/programs/223/add_file 作为 POST

https:/domain/programs/223/add_file as POST

但在提交时我收到错误

没有路由匹配 [POST] "/223/add_file"

No route matches [POST] "/223/add_file"

即使我的地址栏中的内容是完整的路线.如果将完整的路由作为 GET 请求提交,它就可以正常工作.当我运行 rake 路线时,路线显示得很好.

even though what's in my address bar is the complete route. And if submit the complete route as a GET request it works fine. When I run rake routes the route shows up just fine.

这是我的路线的一个子集:

Here is a subset of my route:

resources :programs do
  match "add_file" => "programs#add_file"

如果重要的话,我在 Apache 上使用 Passenger 运行 Rails 3.2.2.问题仅发生在此生产服务器上,从未发生在开发中.

If it matters, I'm running Rails 3.2.2 with Passenger on Apache. The problem only happens on this production server, never in development.

有什么想法吗?我一直坚持这个,因为它会影响多条路线,我已经尝试为该表单定义一个自定义路线,但没有运气.

Any ideas? I'm stuck on this one as it effects multiple routes and I've tried defining a custom route just for that form with no luck.

更新:当我从表单中删除 multi-part => true 或 file_field_tag 时,它解决了问题.这仍然是一个问题,但似乎与路由有关,而不是与带有文件上传的表单有关.

Update: When I remove multi-part => true or the file_field_tag from the form it fixes the problem. It's still an issue but seems to be less about routing than about the form with file uploads.

推荐答案

lib 文件夹中使用以下代码创建 passenger_extension.rb:

Create passenger_extension.rb in the lib folder with this code:

乘客 3

module PhusionPassenger
  module Utils

    protected

    NULL = "\0".freeze

    def split_by_null_into_hash(data)
      args = data.split(NULL, -1)
      args.pop
      headers_hash = Hash.new
      args.each_slice(2).to_a.each do |pair|
        headers_hash[pair.first] = pair.last unless headers_hash.keys.include? pair.first
      end
      return headers_hash
    end

  end
end

乘客 5

module PhusionPassenger
  module Utils

    # Utility functions that can potentially be accelerated by native_support functions.
    module NativeSupportUtils
      extend self

      NULL = "\0".freeze

      class ProcessTimes < Struct.new(:utime, :stime)
      end

      def split_by_null_into_hash(data)
        args = data.split(NULL, -1)
        args.pop
        headers_hash = Hash.new
        args.each_slice(2).to_a.each do |pair|
          headers_hash[pair.first] = pair.last unless headers_hash.keys.include? pair.first
        end
        return headers_hash
      end

      def process_times
        times = Process.times
        return ProcessTimes.new((times.utime * 1_000_000).to_i,
          (times.stime * 1_000_000).to_i)
      end
    end

  end # module Utils
end # module PhusionPassenger

然后在'config/application.rb'中做:

And then in 'config/application.rb' do:

class Application < Rails::Application
  ...
  config.autoload_paths += %W(#{config.root}/lib)
  require 'passenger_extension'
end

然后重新启动网络服务器.

And then restart a webserver.

注意:我不确定这不会破坏任何其他功能,因此请自行承担风险,如果您发现这种方法有任何危害,请告诉我.

NOTICE: I'm not sure that this doesn't break any other functionality so use it on your own risk and please let me know if you find any harm from this approach.

这篇关于Post/Put 请求的路由错误(Passenger Headers)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆