Note that there are some explanatory texts on larger screens.

plurals
  1. PORails3 phonegap jsonp method override PUT,POST
    primarykey
    data
    text
    <p>I am trying to create a phone gap app using the rails 3 rest api. Because of the cross domain i have to use JSONP which only allows GET. But is rails you can put _method=POST in the form action to override the method</p> <p>I also need to override the rack middleware MehtodOVerride with my own custom middleware to allow it.</p> <p>So I created a lib/restful_jsonp_middleware.rb with following code </p> <pre><code>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 </code></pre> <p>Also I added these lines to my application.rb file</p> <pre><code>require 'lib/restful_jsonp_middleware.rb' config.middleware.swap Rack::MethodOverride,Rack::RestfulJsonpMiddleware </code></pre> <p>Now if i do rack middleware i am getting this error</p> <pre><code>rake aborted! </code></pre> <p>cannot load such file -- lib/restful_jsonp_middleware.rb /home/sunny/workspace/ruby_projects/jquery_bootstrap/config/application.rb:4:in <code>require' /home/sunny/workspace/ruby_projects/jquery_bootstrap/config/application.rb:4:in</code>' /home/sunny/workspace/ruby_projects/jquery_bootstrap/Rakefile:5:in <code>require' /home/sunny/workspace/ruby_projects/jquery_bootstrap/Rakefile:5:in</code>' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in <code>load' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in</code>load_rakefile' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:501:in <code>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</code>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 <code>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</code>load_rakefile' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:65:in <code>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</code>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 <code>run' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in</code>' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in <code>load' /home/sunny/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in</code>' /home/sunny/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in <code>eval' /home/sunny/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in</code>'</p> <p>What am I doing wrong.</p> <p>Can anyone please help me</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload