A
A
Anton Ipatov2019-01-23 14:58:30
Ruby on Rails
Anton Ipatov, 2019-01-23 14:58:30

Sortable list in Rails?

I need to add the ability to sort images by their position like in the Railscasts
example I'm using ruby ​​2.5.0, rails 5.2.2, mongoid, 'jquery-ui-rails' and Attachinary ( https://github.com/ipatovanton/attachinary/tree /po... to upload an image
projects_controller.rb

class ProjectsController < ApplicationController
  def sort
    image = Attachinary::File
    params[:image].each_with_index do |id, index|
      image.update_all(:position, index+1) if image
    end
    render nothing: true
  end
end

project.rb
class Project
  include Mongoid::Document

  has_attachments :images
end

routes.rb
resources :projects do
  collection do
    post :sort
  end
end

application.js
jQuery(function() {
  $(document).on('turbolinks:load', function(){
    $('.attachinary-input').attachinary()

    $("#images").sortable({
      update: function(e, ui) {
        Rails.ajax({
          url: $(this).data("url"),
          type: "POST",
          data: $(this).sortable('serialize'),
        });
      }
    });
  });
})

show.html.erb
<div id="images" class="grid" data-url="<%= sort_projects_path %>">
  <% @project.images.order(id: :asc ).each do |image| %>
    <div id="image_<%= image.id %>" class="box">
      <div class="box-image">
        <%= cl_image_tag(image.path, width: '250', height: '250', crop: 'thumb') %>
      </div>
      <%= image %>
      <%= Attachinary %>
    </div>
  <% end %>
</div>

When I try to move an image I get the following error
Started POST "/projects/sort" for 127.0.0.1 at 2019-01-22 23:10:08 +0300 Processing by ProjectsController#sort as / Parameters: {"image"=>["5c472e4c1996da1d037f5810", "5c472e4c1996da1d037f580f", "5c472e4c1996da1d037f5811", "5c472e4c1996da1d037f5812", "5c472e4c1996da1d037f5813", "5c472e4c1996da1d037f5814", "5c472e4c1996da1d037f5815", "5c472e4c1996da1d037f5816", "5c472e4c1996da1d037f5817", "5c472e4c1996da1d037f5818", "5c472e4c1996da1d037f5819", "5c472e4c1996da1d037f581a", "5c472e4c1996da1d037f581b", "5c472e4c1996da1d037f581c", "5c472e4c1996da1d037f581d", "5c472e4c1996da1d037f581e", "5c472e4c1996da1d037f581f", "5c472e4c1996da1d037f5820"]} MONGODB | localhost:27017 | squarely_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('5c472c2e1996da1d037f57fb')}, "sort"=>{"_id"=>1}, "limit"=>1, "singleBatch"=>true, "lsid"=>{"id"=>}} MONGODB | localhost:27017 | squarely_development.find | SUCCEEDED | 0.005s Completed 500 Internal Server Error in 12ms

    ArgumentError (wrong number of arguments (given 2, expected 0..1)):

    app/controllers/projects_controller.rb:34:in block in sort'
      app/controllers/projects_controller.rb:33:ineach' app/controllers/projects_controller.rb:33:in each_with_index'
      app/controllers/projects_controller.rb:33:insort'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Grudinkin, 2019-01-23
@IpatovAnton

1) 2) Why if in an iterator if an external variable is checked?image.update_all(position: index + 1)

if image
    params[:image].each_with_index do |id, index|
      image.update_all(position: index+1) 
    end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question