title: BackBone client with a remote Rails Server
body: <p><strong>Background:</strong></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>My backbone client is only few lines:</p>
  
  <pre><code>Hotel = Backbone.Model.extend({
    initialize: function(){
      console.log("initialize Hotel")
    }
  
  });
  
  Hotels = Backbone.Collection.extend({
    model: Hotel,
    url: 'http://0.0.0.0:3000/hotels'
  });
  </code></pre>
  
  <p>But when i fetch hotels with backbone, rails responds with the <strong>format.html</strong> and not the <strong>format.json</strong> which Backbone can parse.</p>
  
  <p><strong>hotels_controller.rb</strong></p>
  
  <pre><code># 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
  </code></pre>
  
  <p><strong>Safari inspector console:</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Updated with url set to url: "http://0.0.0.0:3000/hotels.json</p>
  
  <p>it can fatch but can't make others CRUD (example PUT)</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Instead if i set only /hotels it works (but the backbone client must reside on the server)</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>uploaded the code on github</p>
  
  <p><a href="https://github.com/RevH/backbonefails" rel="nofollow">https://github.com/RevH/backbonefails</a></p>
  
  <p><em><strong>EDIT 3:</em></strong></p>
  
  <p>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.</p>
  
  <p>i open a rails issue on github at <a href="https://github.com/rails/rails/issues/5005" rel="nofollow">https://github.com/rails/rails/issues/5005</a></p>