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


Software Compatibility Best Practices

I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.

You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.

Get the Guide Now