Open msolovyov opened 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.
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.
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
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}"
backbone models will still have an id property, it just uses the "_id" attribute to modify how it parses json responses from the server.
well... they seemed not to work, prior to me making the above changes.
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.
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.
@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).
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
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:
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
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