itmammoth / rails_sortable

Easy drag & drop sorting with persisting the arranged order for rails
MIT License
143 stars 37 forks source link

After update to 1.2.0 undefined method `each_with_index' for nil:NilClass #24

Closed nik-kg closed 6 years ago

nik-kg commented 6 years ago

In any model when try to sort:

http://localhost:3000/admin/teams -> /sortable/reorder
Started POST "/sortable/reorder" for 127.0.0.1 at 2018-05-21 14:51:47 +0600
Processing by SortableController#reorder as */*
  Parameters: {"Team"=>["6", "5", "7", "8", "9", "12", "11"]}
   (0.2ms)  BEGIN
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.7ms)

NoMethodError (undefined method `each_with_index' for nil:NilClass):

rails_sortable (1.2.0) app/controllers/sortable_controller.rb:7:in `block in reorder'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/home/nbook/.rbenv/versions/2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:210:in `transaction'
rails_sortable (1.2.0) app/controllers/sortable_controller.rb:6:in `reorder'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
turnout (2.4.1) lib/rack/turnout.rb:25:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.5) lib/rack/etag.rb:25:in `call'
rack (2.0.5) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.5) lib/rack/head.rb:12:in `call'
rack (2.0.5) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.5) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.5) lib/rack/method_override.rb:22:in `call'
rack (2.0.5) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
rack (2.0.5) lib/rack/handler/webrick.rb:86:in `service'
/home/nbook/.rbenv/versions/2.5.0/lib/ruby/2.5.0/webrick/httpserver.rb:140:in `service'
/home/nbook/.rbenv/versions/2.5.0/lib/ruby/2.5.0/webrick/httpserver.rb:96:in `run'
/home/nbook/.rbenv/versions/2.5.0/lib/ruby/2.5.0/webrick/server.rb:307:in `block in start_thread'
127.0.0.1 - - [21/May/2018:14:51:47 DST] "POST /sortable/reorder HTTP/1.1" 500 12603
http://localhost:3000/admin/teams -> /sortable/reorder

application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui/widgets/sortable
//= require turbolinks
//= require jquery.ui.widget

(function($) {

  $.fn.NewSortable = function(options) {

    var setting = $.extend(options);
    setting["update"] = function() {
      $.post("/sortable/reorder", $(this).sortable('serialize'))
    }

    this.sortable(setting);
  };

})(jQuery);

application_controller.rb

  before_action :check_sortable

  private 

  def check_sortable
    if request.path == '/sortable/reorder' and !admin_signed_in?
      redirect_to root_path
    end
  end

team.rb (model)

include RailsSortable::Model

set_sortable :position 

include Rails.application.routes.url_helpers

schema

  create_table "teams", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.string "image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "position"
  end

1.1.3 works perfect.

itmammoth commented 6 years ago

There were some changes in parsing parameters process on SortableController. 6f743638ce3777cf7c2300aa22eb2371956a414c

You don't have to define a sortable function on your way like NewSortable, because this plugin provides you the $.fn.railsSortable.

Please try this.

$(function() {
  $('.sortable').railsSortable();
});
nik-kg commented 6 years ago

@itmammoth I'm sorry. Forget to say about this: i define it in my view:

<div class="sortableTeams">
    <% @teams.order(:position).each_with_sortable_id do |team, sortable_id| %>
        <div class="team-one" id="<%= sortable_id %>">

[.....]

<script type="text/javascript">
    $(document).ready(function() {
        $('.sortableTeams').NewSortable();
    });
</script>
itmammoth commented 6 years ago

Please use railsSortable,

//= require jquery-ui/widgets/sortable
//= require rails_sortable <- You need this line
$('.sortableTeams').railsSortable();

instead of

$('.sortableTeams').NewSortable();
itmammoth commented 6 years ago

I closed this issue. Feel free to reopen it if there still be any problems.