Ruby on rails 具有两个单词名称的模型出现错误:未捕获类型错误:无法调用方法';绑定&x27;未定义的

Ruby on rails 具有两个单词名称的模型出现错误:未捕获类型错误:无法调用方法';绑定&x27;未定义的,ruby-on-rails,backbone.js,gem,coffeescript,Ruby On Rails,Backbone.js,Gem,Coffeescript,我将按照这个示例介绍主干rails gem。它适用于只有一个单词名称的模型,但我得到了未捕获的TypeError:undefined的方法'bind',如果我尝试对具有两个名称的模型采用相同的模式,则无法调用undefined的方法。以下是我所做的: rails new blog 然后我将rails主干gem添加到gem文件中 bundle install rails g backbone:install rails g scaffold FriendRequest sender_gend

我将按照这个示例介绍主干rails gem。它适用于只有一个单词名称的模型,但我得到了未捕获的TypeError:undefined的方法'bind',如果我尝试对具有两个名称的模型采用相同的模式,则无法调用undefined的方法。以下是我所做的:

rails new blog
然后我将
rails主干
gem添加到gem文件中

bundle install

rails g backbone:install

rails g scaffold FriendRequest sender_gender:string recipient_gender:string

rake db:migrate

rails g backbone:scaffold FriendRequest sender_gender:string recipient_gender:string
我的
app/views/friend\u requests/index.html.erb
文件如下所示:

<h1>Listing friend_requests</h1>

<table>
  <tr>
    <th>Sender gender</th>
    <th>Recipient gender</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @friend_requests.each do |friend_request| %>
  <tr>
    <td><%= friend_request.sender_gender %></td>
    <td><%= friend_request.recipient_gender %></td>
    <td><%= link_to 'Show', friend_request %></td>
    <td><%= link_to 'Edit', edit_friend_request_path(friend_request) %></td>
    <td><%= link_to 'Destroy', friend_request, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Friend request', new_friend_request_path %>


<div id="friend_requests"></div>

<script type="text/javascript">
  $(function() {
    // Blog is the app name
    window.router = new Blog.Routers.FriendRequestRouter({friend_requests: <%= @friend_requests.to_json.html_safe -%>});
    Backbone.history.start();
  });
</script>
我从控制台复制了代码,控制台中显示了错误。我不确定StackOverflow的正确协议是否存在这样的错误,但您可以看到,它被*包围。如果有人想编辑此问题以正确显示,将不胜感激

(function() {
  var _base,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  (_base = Blog.Views).FriendRequests || (_base.FriendRequests = {});

  Blog.Views.FriendRequests.IndexView = (function(_super) {

    __extends(IndexView, _super);

    function IndexView() {
      this.render = __bind(this.render, this);

      this.addOne = __bind(this.addOne, this);

      this.addAll = __bind(this.addAll, this);
      return IndexView.__super__.constructor.apply(this, arguments);
    }

    IndexView.prototype.template = JST["backbone/templates/friend_requests/index"];

    IndexView.prototype.initialize = function() {
      return this.options.friendRequests.bind('reset', this.addAll);
***Uncaught TypeError: Cannot call method 'bind' of undefined***
    };

    IndexView.prototype.addAll = function() {
      return this.options.friendRequests.each(this.addOne);
    };

    IndexView.prototype.addOne = function(friendRequest) {
      var view;
      view = new Blog.Views.FriendRequests.FriendRequestView({
        model: friendRequest
      });
      return this.$("tbody").append(view.render().el);
    };

    IndexView.prototype.render = function() {
      $(this.el).html(this.template({
        friendRequests: this.options.friendRequests.toJSON()
      }));
      this.addAll();
      return this;
    };

    return IndexView;

  })(Backbone.View);

}).call(this);
最后,生成上述错误的coffeescript文件
index_view.js.coffee

Blog.Views.FriendRequests ||= {}

class Blog.Views.FriendRequests.IndexView extends Backbone.View
  template: JST["backbone/templates/friend_requests/index"]

  initialize: () ->
    @options.friendRequests.bind('reset', @addAll)

  addAll: () =>
    @options.friendRequests.each(@addOne)

  addOne: (friendRequest) =>
    view = new Blog.Views.FriendRequests.FriendRequestView({model : friendRequest})
    @$("tbody").append(view.render().el)

  render: =>
    $(@el).html(@template(friendRequests: @options.friendRequests.toJSON() ))
    @addAll()

    return this
我假设这只是一件简单的事情,比如对于有两个名字的模型不使用适当的大写或其他约定,但我似乎无法理解

提前谢谢

编辑-----

我刚刚想到了这一点,但我会密切关注我的
app/views/friend\u requests/index.html.erb
文件,因为它是我手工编辑代码的地方。其他所有内容都是由
rails主干
gem自动生成的

--------编辑#2--------------

根据询问IndexView实例化位置的第一条评论,它位于
/app/assets/javascripts/backbone/routers/friend\u requests\u router.js.coffee

class Blog.Routers.FriendRequestsRouter extends Backbone.Router
  initialize: (options) ->
    @friendRequests = new Blog.Collections.FriendRequestsCollection()
    @friendRequests.reset options.friendRequests

  routes:
    "new"      : "newFriendRequest"
    "index"    : "index"
    ":id/edit" : "edit"
    ":id"      : "show"
    ".*"        : "index"

  newFriendRequest: ->
    @view = new Blog.Views.FriendRequests.NewView(collection: @friend_requests)
    $("#friend_requests").html(@view.render().el)

  index: ->
    @view = new Blog.Views.FriendRequests.IndexView(friend_requests: @friend_requests)
    $("#friend_requests").html(@view.render().el)

  show: (id) ->
    friend_request = @friend_requests.get(id)

    @view = new Blog.Views.FriendRequests.ShowView(model: friend_request)
    $("#friend_requests").html(@view.render().el)

  edit: (id) ->
    friend_request = @friend_requests.get(id)

    @view = new Blog.Views.FriendRequests.EditView(model: friend_request)
    $("#friend_requests").html(@view.render().el)
您的路由器执行以下操作:

index: ->
  @view = new Blog.Views.FriendRequests.IndexView(friend_requests: @friend_requests)
  #...
注意
friend\u请求:
部分。但是,您的
Blog.Views.FriendRequests.IndexView始终在寻找
@选项。FriendRequests

initialize: () ->
  @options.friendRequests.bind('reset', @addAll)
主干将负责:

创建新视图时,您传递的选项将作为
this.options
附加到视图,以供将来参考

但它不会将带下划线的名称转换为大小写的名称。您的视图应该查看
@options.friend\u requests
而不是
@options.friendRequests
(或者在实例化视图时使用
friendRequests:
)。对于变量,您可能希望只使用一种命名约定(小写和下划线或驼峰大小写)。下划线在Ruby和Rails中更常见,所以这可能是一个不错的选择(OTOH,camel case在JavaScript中非常常见,所以…)

注:主干视图中已经有jQuery版本的
@el
,因此您不必在任何地方使用
$(@el)
,只需使用
@$el


主干网1.1.0更新:从1.1.0开始,主干网视图不再将此设置为
。选项
自动设置为视图的选项,如果您需要该行为,则必须自己执行:

initialize: (options) ->
  @options = options
  #...
或:

您还可以从
选项
中获取所需的选项,然后扔掉其余的选项:

initialize: (options) ->
  @friend_requests = options.friendRequests
  #...

我更喜欢后一种方法,因为它使您的视图选项非常明确且易于跟踪。

您在哪里实例化您的
Blog.Views.FriendRequests.IndexView
?请参见编辑#2:我相信这是在friend#u requests_router.js.coffeehah中,是的,我向某人展示了这一点,并评论了在一个文件中有friend#请求,friendRequests和friendRequests.:)我会把它修好,然后把支票寄出去。谢谢最难看到的错误是那些盯着你看的错误。
initialize: (@options) ->
  #...
initialize: (options) ->
  @friend_requests = options.friendRequests
  #...