我正在尝试使用 Rails3 和咖啡脚本实现排序列表。我之前遇到了路由问题,在您的慷慨帮助下能够解决它,所以现在我遇到了另一个问题,我的数据参数没有传递到服务器,
以下是我的看法
#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('/books/sort', $(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
这是我得到的错误
Served asset /jquery.js - 304 Not Modified (0ms)
Started POST "/books/sort" for 127.0.0.1 at 2012-11-28 17:03:25 +0530
Processing by BooksController#sort as */*
Book Load (0.2ms) SELECT "books".* FROM "books"
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `index' for nil:NilClass):
app/controllers/books_controller.rb:14:in `block in sort'
app/controllers/books_controller.rb:13:in `each'
app/controllers/books_controller.rb:13:in `sort'
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (40.8ms)
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (47.7ms)
所以当我说p params
,我明白{"action"=>"sort", "controller"=>"books"}
了,所以它没有一个名为“book”的参数,因此给出了一个 nil 错误,有人可以帮助我吗?
任何帮助将不胜感激
提前致谢