Rails 3 rest api を使用して電話ギャップ アプリを作成しようとしています。クロスドメインのため、GET のみを許可する JSONP を使用する必要があります。しかし、メソッドをオーバーライドするためにフォームアクションに _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
block 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:インチ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
'
私は何を間違っていますか。
誰でも私を助けてくれませんか