Backbone + Rails:无法调用未定义的方法'create'

时间:2013-08-02 17:53:52

标签: javascript ruby-on-rails backbone.js coffeescript

您好,我有一个新的骨干,我似乎无法让它在我的Rails应用程序中工作。

我正在尝试在“posts / new”页面中为集合创建模型。

posts_new.js.coffee

class Poster.Views.PostsNew extends Backbone.View

  template: JST['posts/new']

  events:
    'submit #new_post': 'createPost'

  render: ->
    $(@el).html(@template(post: "NEW PAGE"))
    this

  createPost: (event) ->
    event.preventDefault()
    if $('#new_post_title').val() == '' || $('#new_post_message').val() == '' 
        alert "Please fill out form"
        $('#new_post')[0].reset()
    else @collection.create(title: $('#new_post_title').val(), message: $('#new_post_message').val()); $('#new_post')[0].reset(); Backbone.history.navigate("/posts", true)

这是我的 posts_routes.js.coffee ,其中我传递了@collection变量

class Poster.Routers.Posts extends Backbone.Router
    routes:
        'posts':'index'
        '':'index'
        'posts/new': 'new'
        'posts/:id': 'show'

    initialize: ->
        @collection = new Poster.Collections.Posts()
        @collection.fetch({reset: true})


    index: ->
        view = new Poster.Views.PostsIndex(collection: @collection)
        $('#index_container').html(view.render().el)

    show: (id) ->
        view = new Poster.Views.PostsShow()
        $('#index_container').html(view.render().el)

    new: ->
        view = new Poster.Views.PostsNew(collection: @collection)
        $('#index_container').html(view.render().el)

0 个答案:

没有答案