Сортируемый список в Rails?

Мне нужно добавить возможность сортировать изображения по их положению как в примере Railscasts

Я использую ruby 2.5.0, rails 5.2.2, mongoid, 'jquery-ui-rails' и Attachinary (https://github.com/ipatovanton/attachinary/tree/po... для загрузки изображении.

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>


Когда я пытаюсь переместить изображение, то получаю следующую ошибку

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'
  • Вопрос задан
  • 81 просмотр
Решения вопроса 1
1) image.update_all(position: index + 1)

2) Зачем if в итераторе, если проверяется внешняя переменная?
if image
    params[:image].each_with_index do |id, index|
      image.update_all(position: index+1) 
    end
end
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы