I found this screencast by Ryan Bates:
147 Sortable Lists (Revisited)
The screencast is a bit dated, it dates from 2011 and was made with Rails 3.1
I followed it along, and I’m able to drag and drop the table rows of my admin panel, but the changes don’t get persisted to the database.
My view template looks like this:
app/views/admin/sections/index.html.erb
<section class="dashboard">
<%= button_to "Sectie Toevoegen", new_admin_section_path, method: "get" %>
<table id="sections" class="table-borders">
<tbody data-update-url="<%= sort_admin_sections_url %>">
<% @sections.each do |section| %>
<%= content_tag_for :tr, section do %>
<td><%= link_to section.title, admin_section_path(section) %></td>
<td><%= link_to "Aanpassen", edit_admin_section_path(section) %> <%= link_to 'Verwijderen', admin_section_path(section), method: :delete, data: { confirm: "Ben je zeker?" } %></td>
<% end %>
<% end %>
</tbody>
</table>
</section>
The coffeescript file with that handles the Javascript looks like this:
app/assets/javascripts/sections.js.coffee
jQuery ->
$('#sections tbody').sortable
axis: 'y'
update: ->
$.post($(this).data('update_url'), $(this).sortable('serialize'))
I tested the ‘update’ callback with a simple alert() to check if the update callback gets triggered when a table row is dragged and dropped, and that worked
On my controller, the sort() method which handles persistance to the database of a changed position, looks like this:
app/controllers/admin/sections_controller.rb
def sort
params[:section].each_with_index do |id, index|
Section.where(id: id).update_all({position: index+1})
end
render nothing: true
end
I think the problem must be located in the sort action, when I tail my development.log, I see this 500 server error, when dragging a table row:
Started POST "/admin/sections" for 127.0.0.1 at 2014-11-13 21:40:46 +0100
Processing by Admin::SectionsController#create as */*
Parameters: {"section"=>["3", "4", "6", "2", "5"]}
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `permit' for ["3", "4", "6", "2", "5"]:Array):
app/controllers/admin/sections_controller.rb:54:in `section_params'
app/controllers/admin/sections_controller.rb:15:in `create'
Finaly my sort route looks like this:
namespace :admin do
...
resources :sections do
collection { post :sort }
end
end
I guess the problem must be caused by the new mass assignment security features in Rails4, but I’m not sure how to fix it.
it tried to add ‘:position => ’ to my private security_params method:
private
def section_params
params.require(:section).permit(:title, :description, :image, :category_id, :position => [])
end
But that didn’t fix it.
Any help is much appreciated, my repository is at:
https://github.com/acandael/beautysalonapp2/tree/drag-and-drop
greetings,
Anthony