Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:07:13+00:00 2026-06-15T03:07:13+00:00

I’m trying to implement a sorting list with Rails3 and coffeescript. I had a

  • 0

I’m trying to implement a sorting list with Rails3 and coffeescript. I had a routing issue earlier and was able to solve it with your generous help, so now I have a different issue, My data parameter is not passing to the server,

Following is my view

#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 %>

following is my books.js.coffee file

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

Following is my controller

#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

this is the error I’m getting

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)

So when i say p params, I get {"action"=>"sort", "controller"=>"books"}, So it doesnt have a param called ‘book’ and hence gives a nil error, can someone help me out,

any help will be greatly appreciated

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:07:14+00:00Added an answer on June 15, 2026 at 3:07 am

    You have a couple things going wrong here. First of all, your $.post arguments are a bit confused:

    $.post('/books/sort', $(this).data('#books'), $(this).sortable('serialize'))
    

    $.post wants the data in the second argument so just this:

    $.post('/books/sort', $(this).sortable('serialize'))
    

    makes more sense.

    Sortable’s serialize method wants DOM id attributes to be in a specific form:

    It works by default by looking at the id of each item in the format "setname_number", and it spits out a hash like "setname[]=number&setname[]=number".

    In your Rails controller, you want an array of book IDs in params[:book] so you want book[]=id&book[]=id&... going to your server. That means that you want your <li>s to look more like this:

    <li id="book_<%= book.id %>">
    

    Or you could keep use class attributes by including an underscore:

    <li class="book_<%= book.id %>">
    

    and then telling serialize to look at class:

    $(this).sortable('serialize', attribute: 'class')
    

    You should also use the expression option and leave the HTML alone:

    $(this).sortable('serialize', attribute: class, expression: /(book)(.+)/)
    

    There are a few other ways to arrange things, see the sortable documentation for further details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.