codebrew / backbone-rails

Easily use backbone.js with rails 3.1
MIT License
1.62k stars 257 forks source link

id is not defined, when running the scaffold example #5

Open msolovyov opened 13 years ago

msolovyov commented 13 years ago

After I create a post, then go back to /posts, I get the following error in console and nothing displayed on the page

id is not defined at

codebrew commented 13 years ago

Is the post actually being created and saved on the server side? It sounds like an error is probably happening, and right now the default scaffolding setup doesn't show any messages when an error happens. Its something I definitely want to fix soon.

matthewrudy commented 12 years ago

I have a similar error but I think it's just because I am using Mongoid. (the json comes with an "_id", not an "id")

I think I need to override the json representation.

codebrew commented 12 years ago

backbone.js lets you set what the id attribute should be in the response. On the model you can define something like idAttribute: "_id" and it should all work

matthewrudy commented 12 years ago

cool, that works. there are also some other references in the generated scaffold code which reference id.

here are my rough changes

// my_model.js.coffee
class MyApp.Models.MyModel extends Backbone.Model
  idAttribute: "_id" 

// my_model.jst.ejs
<td><a href="#/<%= _id %>">Show</td>
<td><a href="#/<%= _id %>/edit">Edit</td>
<td><a href="#/<%= _id %>/destroy" class="destroy">Destroy</a></td>

// edit_view.js
update: (e) ->
  ...
  success:(model) =>
    @options.model = model
    window.location.hash = "/#{@options.model._id}"

// new_view.js
save:
  ...
  success: (model) =>
    @options.model = model
    window.location.hash = "/#{@options.model._id}"
codebrew commented 12 years ago

backbone models will still have an id property, it just uses the "_id" attribute to modify how it parses json responses from the server.

matthewrudy commented 12 years ago

well... they seemed not to work, prior to me making the above changes.

codebrew commented 12 years ago

Its possible that backbone changed the way this is handled, I'll have to look over the source to see.

Either way Id definitely be up for a patch where you can pass the generator if its a mongoid model, or simply what the id attribute should be.

matthewrudy commented 12 years ago

I need to do some more investigation. I'm just playing right now. Deciding how to proceed.

Will let you know if I find time to do any work on it. Thanks for your help.

HashNuke commented 12 years ago

@matthewrudy thanks for the pointers! I too had to change id to _id in the templates to get it working. just setting the idAttribute in the model didn't do. \cc: @codebrew

If it's going to help I'm on rails 3.1.1, mongoid and master branch of rails-backbone (using git source).

matthewrudy commented 12 years ago

Hey. I'd actually totally forgotten about this. Ended up being dragged onto an Android project.

Hope I'll have time to get back onto Backbone in the next few months.

On 15 November 2011 19:08, Akash Manohar < reply@reply.github.com

wrote:

@matthewrudy thanks for the pointers! I too had to change id to _id in the templates to get it working. just setting the idAttribute in the model didn't do. \cc: @codebrew


Reply to this email directly or view it on GitHub: https://github.com/codebrew/backbone-rails/issues/5#issuecomment-2743469

mankind commented 12 years ago

An alternative option is to change it serverside by overidding either the as_json method or the serializable_hash method, as described in the two links below:

http://blog.joshdzielak.com/blog/2011/12/24/tame-the-mongoid-id-field-in-your-rails-and-backbone-js-app/


  module Mongoid
    module Document
      def as_json(options={})
        attrs = super(options)
        attrs["id"] = self.persisted? ? self._id : nil
        attrs
      end
    end
 end

https://github.com/mongoid/mongoid/issues/1262


  module Mongoid
    module BackboneSerialization
      extend ActiveSupport::Concern
      module InstanceMethods
       def serializable_hash(options = nil)
          persisted? ? super.merge('id' => _id) : super
        end
      end
    end
  end