BackBone client with a remote Rails Server

后端 未结 3 1768
面向向阳花
面向向阳花 2021-02-06 18:57

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

3条回答
  •  攒了一身酷
    2021-02-06 19:37

    you've set up your rails code so that it requires a call to /hotels.json in order to return json, but your backbone code is calling /hotels only.

    the easiest way to fix this is to have a separate api for json data, than for html pages. for example: /hotels returns html and /api/hotels return json. see Ryan Bate's Railscasts for an example of this (paid) http://railscasts.com/episodes/323-backbone-on-rails-part-1

    another option would be to change your Backbone models / collections so that they append the ".json" to the end of your urls. for example:

    
    Backbone.Model.extend({
      url: function(){
        var url;
        if (this.isNew()){
          url = "/hotels/" + this.id + ".json";
        } else {
          url = "/hotels.json";
        }
        return url;
      }
    });
    

    There are likely other options as well. These are just two that came up off the top of my head.

提交回复
热议问题