问题
I'm trying to load a resource with sencha touch on rails but i get the following error:
Started OPTIONS "/menu_items.json?_dc=1322512349038&limit=25&sort=%5B%7B%22property%22%3A%22name%22%2C%22direction%22%3A%22ASC%22%7D%5D" for 127.0.0.1 at 2011-11-28 18:32:29 -0200
ActionController::RoutingError (No route matches [OPTIONS] "/menu_items.json"):
My store code:
new Ext.data.Store({
model: 'MenuItem',
sorters: 'name',
getGroupString: function(r){
return r.get('name')[0] || "";
},
proxy: {
type: 'rest',
url: 'http://localhost:3000/menu_items',
format: 'json',
reader: {
type: 'json',
root: 'menu_item'
}
},
listeners: {
load: { fn: this.initializeData, scope: this }
}
})
回答1:
If your Rails code looks something like below, then the problem is probably the OPTIONS
method that is used - the index
action only responds to GET
. Do you know why OPTIONS
is used here? I can't find this in the Sencha Touch docu for the data store..
# config/routes.rb
# ...
resources :menu_items
# app/controllers/menu_items_controller.rb
class MenuItemsController < ApplicationController
def index
@menu_items = MenuItem.all
respond_to do |format|
format.json { render :json => @menu_items }
end
end
end
BTW: from what I see here, you should maybe move the proxy code into the MenuItem
model.
回答2:
The OPTION
request is part of the Cross-Origin Resource Sharing (CORS) protocol. Check this valuable post about it. You can use the rack-cors gem to easily configure it.
来源:https://stackoverflow.com/questions/8301957/sencha-touch-rails-3-1