问题描述
背景:
我用导轨支架摹酒店名称星级:整数启动快(并插入到数据库中的某些记录)
谱写骨干客户Rails应用程序之外。
I use "rails g scaffold hotel name stars:integer" to start fast (and insert into the database some records),and write a Backbone client outside the rails app.
我在本地打开骨干客户端Safari浏览器文件:///Users/lg/Workspace/www/index.html测试客户端,因为我的想法是把铁轨服务器的主机(例如Heroku的。),并插入上骨干客户到PhoneGap的应用程序。
I open the Backbone client locally with Safari file:///Users/lg/Workspace/www/index.html for testing the client because my idea is to put the rails server on a host (ex. Heroku) and insert the Backbone client into a PhoneGap App.
我的客户骨干只有几行:
My backbone client is only few lines:
Hotel = Backbone.Model.extend({
initialize: function(){
console.log("initialize Hotel")
}
});
Hotels = Backbone.Collection.extend({
model: Hotel,
url: 'http://0.0.0.0:3000/hotels'
});
但是当我取骨干酒店,导轨和响应在 format.html 并不会在 format.json 其中骨干可以解析。
But when i fetch hotels with backbone, rails responds with the format.html and not the format.json which Backbone can parse.
hotels_controller.rb
# GET /hotels
# GET /hotels.json
def index
@hotels = Hotel.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @hotels }
end
end
Safari浏览器检查控制台:
hotels = new Hotels()
Object
hotels.fetch()
Object
hotels.length
0
Request URL:http://0.0.0.0:3000/hotels
Request method:GET
Status code:200 OK
Request Headers
Accept:application/json, text/javascript, */*; q=0.01
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10
Response Headers
Cache-Control:max-age=0, private, must-revalidate
Connection:Keep-Alive
Content-Length:2233
Content-Type:text/html; charset=utf-8
Date:Sat, 11 Feb 2012 14:31:52 GMT
Etag:"606da2b7c21ca96c9d71aabccdd439e9"
Server:WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
编辑:
更新了网址设置为URL:http://0.0.0.0:3000/hotels.json
Updated with url set to url: "http://0.0.0.0:3000/hotels.json
它可以混帐,但不能让别人CRUD(例如PUT)
it can fatch but can't make others CRUD (example PUT)
hotels = new Hotels()
Object
hotels.fetch()
Object
hotels.length
5
hotel = hotels.get(2)
Object
hotel.set({name: "name 2"})
Object
hotel.save()
Object
PUT http://0.0.0.0:3000/hotels.json/2 404 (Not Found)
相反,如果我只设置/酒店它的工作原理(但主干客户端必须驻留在服务器上)
Instead if i set only /hotels it works (but the backbone client must reside on the server)
编辑2:
上传code在github
uploaded the code on github
的 修改3:的
EDIT 3:
另一个细节是,如果你插入backboneclient目录到Rails公开目录,并修改为0.0.0.0:3000/hotels.json /酒店它的作品太棒了!但是,如果我的客户端从服务器上的Safari分离并打开它,它需要在URL的末尾以.json。这是很奇怪的。
Another details is if you insert the backboneclient directory into Rails public directory and change 0.0.0.0:3000/hotels.json to /hotels it works fantastic!! But if i separate the client from server and open it with Safari it require .json at the end of the url. this is very strange.
我打开GitHub上一轨问题在
i open a rails issue on github at https://github.com/rails/rails/issues/5005
推荐答案
要了解的重要一点是,在Rails的最佳做法是不使用的接受头。这里有一个很好的书面记录。他们设置的JQuery一个ajaxSetup beforeSend回调放 * / *
在头开始,这是什么神奇的轨道正则表达式希望看到虽然我不能真的说不清为什么,除了他们知道未修改的浏览器请求会一直把它放在那里。这里是你将如何解决您在JQuery中接受头。
then rails throws it away and defaults to text/html mime type. I know, right? Your header happens to match this. The reason most people don't have this problem is that rails "fixes" the default jquery behavior in the rails jquery-ujs. They set a ajaxSetup beforeSend callback in JQuery that puts the */*
at the beginning of the header, which is what the magic rails regex wants to see though I can't really tell why, other than they know that an unmodified browser request will always put it there. Here is how you might fix your accept header in JQuery.
$(function() {
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("accept", "application/json");
}
});
});
这篇关于骨干客户端与远程服务器的Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!