Sortable Lists in Rails 5

In this article, you will learn how to get the railscast episode 147 on sortable lists to work with Rails 5.

Add:

//= require jquery-ui

to application.js.

The content_tag_for was available in Rails 3 and 4. It has been removed from Rails 5. You can install record_tag_helper to use that tag in Rails 5.

gem 'record_tag_helper', '~> 1.0'

Run bundle. Reload the faq page. You will get the error:

couldn't find file 'jquery-ui' with type 'application/javascript'

Add jquery-ui-rails gem to the Gemfile.

gem 'jquery-ui-rails'

Run bundle. This still will not work. I installed jquery-turbolinks gem to workaround turbolinks conflict with drag and drop functionality. This also failed. I had to remove turbolinks from Rails 5 to get it working.

  • Remove turbolinks gem.
  • Remove the turbolinks related code in application layout.
  • Remove the turbo links require statement in application.js.

Now the drag and drop will work. You can download the code from Sortable Lists in Rails 5 without Turbolinks. You don't need acts_as_list gem to fix the problem where a new faq has null value for the position. When a new faq is created, add a before_create action that populates the position attribute to the existing highest position value + 1.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.