Rails3 phonegap jsonp方法覆盖PUT,POST

时间:2012-10-07 22:31:28

标签: ruby-on-rails cordova cross-domain jsonp rack

我正在尝试使用rails 3 rest api创建一个手机间隙应用。由于跨域我必须使用只允许GET的JSONP。但是rails是你可以在表单动作中放置_method = POST来覆盖方法

我还需要使用自己的自定义中间件覆盖机架中间件MehtodOVerride以允许它。

所以我用以下代码

创建了一个lib / restful_jsonp_middleware.rb
module Rack

class RestfulJsonpMiddleware

  HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
  METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
  HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze

  attr_accessor :method_parameter_name

  def initialize(app)
    @app = app
    @method_parameter_name = METHOD_OVERRIDE_PARAM_KEY
  end

  # We check if the method parameter is in the request
  # and set up the request to allow the execution of the
  # overwritten HTTP method
  def call(env)
    req = Request.new(env)
    method = req.params[@method_parameter_name]
    method = method.to_s.upcase
    if HTTP_METHODS.include?(method)
      env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
      env["REQUEST_METHOD"] = method
      env[HTTP_METHOD_OVERRIDE_HEADER] = method
    end
    @app.call(env)
  end
end
end

此外,我将这些行添加到我的application.rb文件

require 'lib/restful_jsonp_middleware.rb'
config.middleware.swap Rack::MethodOverride,Rack::RestfulJsonpMiddleware

现在,如果我做机架中间件,我收到此错误

rake aborted!

无法加载此类文件 - lib / restful_jsonp_middleware.rb /home/sunny/workspace/ruby_projects/jquery_bootstrap/config/application.rb:4:in require' /home/sunny/workspace/ruby_projects/jquery_bootstrap/config/application.rb:4:in' / home / sunny / workspace / ruby​​_projects / jquery_bootstrap / Rakefile:5:in require' /home/sunny/workspace/ruby_projects/jquery_bootstrap/Rakefile:5:in' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in load' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in load_rakefile' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:501:in raw_load_rakefile' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:82:in块中的load_rakefile' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:81:in load_rakefile' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:65:in block in run' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in run' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in load' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in' /home/sunny/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in eval' /home/sunny/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in'

我做错了什么。

任何人都可以帮助我

0 个答案:

没有答案