【问题标题】:Backbone on Rails: Sortable listRails 上的主干:可排序列表
【发布时间】:2012-06-07 23:28:55
【问题描述】:

我正在关注 Backbone Railscasts 和可排序列表 Railscast,但我无法将它们放在一起。

主干模板:

<ul id="faqs" data-update-url="/api/faqs/sort">
</ul>

主干视图:

render: ->
  $(@el).html(@template())
  @collection.each(@appendFaqs)

  $("#faqs").sortable
    axis: 'y'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))

Rails 控制器:

class FaqsController < ApplicationController

  def sort
   params[:faq].each_with_index do |id, index|
      Faq.update_all({position: index+1}, {id: id})
    end
    render nothing: true
  end

输出:

Started POST "/api/faqs/sort" for 127.0.0.1 at 2012-06-03 01:17:20 +0200
Processing by FaqsController#sort as */*
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `each_with_index' for nil:NilClass):
  app/controllers/faqs_controller.rb:28:in `sort'

我知道我的参数是零。但我不明白为什么在我克隆的 railscast 代码项目中它正在工作。

我看到我应该覆盖toJSON方法(骨干),我应该这样走吗?

说实话,我已经失控了。

使用的 Railscast:

【问题讨论】:

  • 我已经更新了关于它的帖子。

标签: ruby-on-rails list backbone.js jquery-ui-sortable


【解决方案1】:

您需要在 &lt;li&gt; 元素上设置一个 id:

<li id="faq_1">...</li>
<li id="faq_2">...</li>
<li id="faq_3">...</li>
<li id="faq_4">...</li>

您应该阅读本文以更好地理解serialize method

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2012-07-18
    • 2013-10-07
    • 2011-01-08
    相关资源
    最近更新 更多