Note that there are some explanatory texts on larger screens.

plurals
  1. POcross domain resource sharing with rails and angular.js
    primarykey
    data
    text
    <p>cant understand how cors work with rails </p> <p>in my routes.rb</p> <pre><code>resources :topics resources :test match '/topics' =&gt; 'topics#index', :constraints =&gt; {:method =&gt; 'OPTIONS'} match '/topics/:id' =&gt; 'topics#show', :constraints =&gt; {:method =&gt; 'OPTIONS'} </code></pre> <p>in application controller</p> <pre><code>after_filter :set_access_control_headers before_filter :cor def set_access_control_headers headers['Access-Control-Allow-Origin'] = '*' headers['Access-Control-Request-Method'] = 'POST, GET' end def cor headers["Access-Control-Allow-Origin"] = "*" headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE OPTIONS}.join(",") headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",") head(:ok) if request.method == 'OPTIONS' end </code></pre> <p>in angular services.js</p> <pre><code>'use strict'; var services = angular.module('angapp.services', ['ngResource']); services.factory('Topic', ['$resource', function($resource) { return $resource('http://localhost:port/topics.json', {},{ query: {method:'GET', params:{ port:':3000'}, isArray:true} }); }]); services.factory('SingleTopic', ['$resource', function($resource) { return $resource('http://localhost:port/topics/:id.json', {id: '@id' },{ get: {method:'GET', params:{ port:':3000', id:'id'}, isArray:false }}); }]); services.factory('multitopicloader',['Topic', '$q', function(Topic, $q){ return function() { var delay = $q.defer(); Topic.query(function(topics){ delay.resolve(topics); }, function() { delay.reject('Unable to get topics'); }); return delay.promise; }; }]); services.factory('topicloader', ['SingleTopic','$route','$q', function(SingleTopic, $route ,$q){ return function() { var delay = $q.defer(); SingleTopic.get({id: $route.current.params.topicID}, function(topic){ delay.resolve(topic); }, function(){ delay.reject('unable to fetch', + $route.current.params.topicID) }); return delay.promise; }; }]); </code></pre> <p>every thing works fine. i implemented cors as per instructions here <a href="https://stackoverflow.com/questions/13910315/rails-backbone-phonegap-cors-not-allowed-by-access-control-allow-origin-erro">Rails, Backbone, PhoneGap, CORS (not allowed by Access-Control-Allow-Origin error)</a> and <a href="http://www.tsheffler.com/blog/?p=428" rel="nofollow noreferrer">http://www.tsheffler.com/blog/?p=428</a></p> <p>I have some questions here</p> <p>when i commented out or removed either resources :topics or match "/topics" it does not work. It works only when both match and resources statement are present in routes.rb. i dont know why rails requires both match and resource statement to work cors .(i also tried adding :constraints => {:method => 'options'} to resources :topic and removing match => "/topics" but it does not work) In rails 4 match function in depreciated. so how can i do it in rails 4 without 'match'</p> <p>i can't understand clearly what cor and set_access_control_headers actually do. whether server responds with headers in cor function for initial request from browser as response and headers in set_access_control_headers in the final response to browser. </p> <p>can someone clearly explain about 'OPTIONS' with regards to 'cors' and how it works with rails. whats the use of this statement head(:ok) if request.method == 'OPTIONS' </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.
    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