BackBone client with a remote Rails Server

**Background:** 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. 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: '' }); 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 inspector console:** hotels = new Hotels() Object hotels.fetch() Object hotels.length 0 Request URL: 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) **EDIT:** Updated with url set to url: " 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 Object PUT 404 (Not Found) Instead if i set only /hotels it works (but the backbone client must reside on the server) **EDIT 2:** uploaded the code on github ***EDIT 3:*** Another details is if you insert the backboneclient directory into Rails public directory and change 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. i open a rails issue on github at
Why don't you just explicitly ask it for json? /hotels.json

以上就是BackBone client with a remote Rails Server的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JSON 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准