0

hereのソート可能なリストのチュートリアルに従っていますが、残念ながら記事は少し古い (しかし非常に便利) であり、実装しようとするといくつかの問題が発生します

Rails 3.2.x
coffeescript 
Jquery

私の問題は、ビューがコントローラーにリクエストを渡さず、代わりにルーティングエラーを与えることです

以下は私の見解です

#index.html.erb 
<h1>Listing books</h1>
<ul id="books"> <% @books.each do |book| %>
  <li class="book<%= book.id %>"><span class="handle">[drag]</span><%= book.name %></li>
<% end %></ul>
<%= link_to 'New book', new_book_path %>

これが私のコーヒースクリプトです

#books.js.coffee
jQuery ->
  $('#books').sortable
    axis: 'y'
    handle: '.handle'
    update: ->
      $.post($(this).data('#books'), $(this).sortable('serialize'))

これが私のコントローラーです

#books_controller.rb
def sort
  @books = Book.all
  @books.each do |book|
    book.position = params['book'].index(book.id.to_s) + 1
    book.save
  end

  render :nothing => true
end

これが私のルートです

#config/routes.rb
resources :books do
  collection do 
    post :sort  
  end
end

これは私が得ているエラーです

2012-11-28 16:13:29 +0530 で 127.0.0.1 の POST "/undefined" を開始しました

ActionController::RoutingError (No route matches [POST] "/undefined"):
  actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.9) lib/rails/engine.rb:479:in `call'
  railties (3.2.9) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /home/sameera/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  /home/sameera/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  /home/sameera/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'


  Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)

どんな助けでも大歓迎です、

ありがとう

4

1 に答える 1

0

次のようなパラメータ $.postが必要だと思います:url

$.post('/books/sort', $(this).data('#books'), $(this).sortable('serialize'))
于 2012-11-28T10:56:01.083 に答える