I am new to Backbone and I’m having problem saving a model to database. When clicking, the field ‘profile’ should be updated to ‘100’, but it is not working:
$ ->
class User extends Backbone.Model
url: -> '/users/' + this.get("id") '.json'
class Users extends Backbone.Collection
model: User
class UserView extends Backbone.View
tagName: "li"
events:
"click" : "changeProfile"
render: -> $(@el).html( @model.get "name" )
changeProfile: ->
$('li').removeClass('selected')
$(@el).addClass('selected')
@model.set( 'profile' : '100' ).save()
users = new Users
users.url = "/users.json"
users.fetch(
success: ->
_.each users.models, (model) ->
view = new UserView( model: model )
$('ul').append view.render()
)
The ‘users’ controller / ‘user’ model were scaffolded (and the controller renders in json). I am using latest versions of both Backbone and Rails. Can somebody help?
(Javascript Console in Chrome register the following error: “Uncaught TypeError: number is not a function”)
Your
urlforUseris missing a+:The last part of that is being interpreted like this:
and since
get('id')is returning a number, you get a “number is not a function” error. Add the missing+to get string concatenation or use CoffeeScript’s interpolation:or