Ian,

Thanks!

Unfortunately, there is no guidance to be had on using rack-cors with Rails 
2. Besides adding the gem to Gemfile:

*gem 'rack-cors', :require => 'rack/cors'*


initialization should likely go in config/environment.rb, like:


>
>
>
>
>
>
>
>
>
> *Rails::Initializer.run do |config|  # rack-cors  config.middleware.use 
> Rack::Cors do    allow do      origins 'someserver.example.com'      
> resource %r{/users/\d+.json},        :headers => ['Origin', 'Accept', 
> 'Content-Type'],        :methods => [:post, :get]    end  endend*


but this produces:


>
> *$ bundle exec rake middlewarerake aborted!uninitialized constant 
> Rack::Cors*


So I'm puzzling over how to use it in Rails 2.

Scott


On Wednesday, October 22, 2014 7:49:18 PM UTC-4, Ian Young wrote:
>
> Scott,
>  
> It looks like Rails is receiving an OPTIONS request and choking on it. 
> This is generally an indication of CORS taking place (on PUT/POSTs with 
> CORS, it first performs a "pre-flight" OPTIONS request to see if the actual 
> request is allowed). I'm not sure what kind of stuff you have  set up in 
> your .htaccess, but either Apache should be completely handling the OPTIONS 
> request and not passing it through, or else you'll probably want to let 
> Rails know how to deal with those requests. I use Rack::Cors and it works 
> like a charm - it's middleware and intercepts the CORS stuff in a way 
> that's usually completely invisible to the rest of the app.
>  
> Ian
>  
>  
> On Wed, Oct 22, 2014, at 03:35 PM, Scott Olmsted wrote:
>
> A client with a Rails 2 app that displays games written in Flash is having 
> them converted to HTML5/Javascript. The team doing the conversions has no 
> problem reading the player's data with a Javascript function like this, 
> which I bound to a button click and ran with the Chrome debugging console 
> open:
>  
>
>
> *function makeGetRequest() {*
> *  var request = new XMLHttpRequest();**  request.open('GET', 
> <...url...>);*
>
> *  // Response handler*
> *  request.onload = function() {*
> *    var text = request.responseText;*
> *    console.log("responseText="+text)**  };*
>
> *  request.send();**}*
>
>  
> I have omitted the code to handle CORS (cross-origin resource sharing), 
> which is set in .htaccess on the server and does not seem to be a problem.
>  
> The following works in development to push data back to the Rails 2 app:
>   
>
>
> *function makePutRequest() {*
> *  var request = new XMLHttpRequest();**  request.open('PUT', 
> <...url...>);*
>
> *  // Response handler*
> *  request.onload = function() {*
> *    var text = request.responseText;*
> *    console.log("responseText="+text)**  };*
>
> *  request.setRequestHeader('Content-type', 'application/json');*
> *  request.send('{"data":"this is updated data player data"}');**}*
>
>  
> but does not work in production. Changing 'PUT' to 'POST' and adding
>  
>
> *  xhr.setRequestHeader('X-HTTP-Method-Override', 'PUT');*
>
>  
> also works in development but not in production.
>  
> The Chrome console says:
>  
>
> *XMLHttpRequest cannot load <...url...>. Invalid HTTP status code 405 *
>
>  
> The Rails 2 production log says:
>  
>
>
> *Processing ApplicationController#index (for xx.xxx.xx.xxx at 2014-10-22 
> 14:31:28) [OPTIONS]ActionController::MethodNotAllowed (Only get and put 
> requests are allowed.):*
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/routing/recognition_optimisation.rb:64:in
>  
> `recognize_path'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/routing/route_set.rb:442:in
>  
> `recognize'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/routing/route_set.rb:437:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/dispatcher.rb:87:in
>  
> `dispatch'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/dispatcher.rb:121:in
>  
> `_call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/dispatcher.rb:130:in
>  
> `build_middleware_stack'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/activerecord/lib/active_record/query_cache.rb:29:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/activerecord/lib/active_record/query_cache.rb:29:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in
>  
> `cache'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/activerecord/lib/active_record/query_cache.rb:9:in
>  
> `cache'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/activerecord/lib/active_record/query_cache.rb:28:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in
>  
> `call'  haml (3.0.25) rails/./lib/sass/plugin/rack.rb:41:in `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/string_coercion.rb:25:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/params_parser.rb:15:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/session/cookie_store.rb:99:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/failsafe.rb:26:in
>  
> `call'  
> /opt/ruby-enterprise-1.8.7-2012.02/lib/ruby/gems/1.8/bundler/gems/rails-31a564b77c73/actionpack/lib/action_controller/dispatcher.rb:106:in
>  
> `call'  passenger (3.0.12) 
> lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'*
>
>  
>  
> It got as far as actionpack, so why is production behaving badly but not 
> development? Is Apache/Passenger mangling the request before it gets there? 
>  
> The app uses the latest version of Rails 2, ree-1.8.7, and Passenger, 
> hosted at RailsPlayground, where it has run without major problems for five 
> years.
>  
> I'm at a dead-end with Google. Anyone got an idea on what is the problem 
> here?
>  
> Thanks much, Scott
>  
>  
>
> -- 
>  -- 
>  SD Ruby mailing list
>  [email protected] <javascript:>
>  http://groups.google.com/group/sdruby
>  --- 
>  You received this message because you are subscribed to the Google Groups 
> "SD Ruby" group.
>  To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
>  For more options, visit https://groups.google.com/d/optout.
>
>  
>  

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to