Clay

Joined

90 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Sortable Drag and Drop

Hi

Ok added as per your suggestion

https://stackoverflow.com/questions/51676832/sortable-drag-and-drop-rails-jquery
Thanks once again have a great one!

Posted in Sortable Drag and Drop

Hi thanks for the suggestions.
Here is the result of the params
"admin/products", "action"=>"sort"} permitted: false>

The Dom it is on the link_to tag within the td. Not sure if it should be moved out but here it is displaying correctly.

Posted in Sortable Drag and Drop

admin/products controller

def sort
    params[:product].each_with_index do |id, index|
        Product.where(id: id).update_all(position: index + 1)
    end
    head :ok
end

products.js

document.addEventListener("turbolinks:load", function() {

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

Admin Products Index

<tbody id="products" data-url="<%= sort_admin_products_path %>">
   <%= render @products %>
 </tbody>

Admin Products - Product

<tr>
  <td><%= link_to product.product_code, admin_product_path(product), id: dom_id(product) %> - (<%= product.position %>)</td>
  <td><%= product.product_name.truncate(45) %></td>
  <td></td>
  <td><%= link_to 'Edit', edit_admin_product_path(product), class: 'btn btn-sm btn-warning' %></td>
  <td><%=button_to "Remove", admin_product_path(product), method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-sm btn-danger' %></td>
</tr>

Posted in Sortable Drag and Drop

Hi Jacob

Thanks for a idea. Here is the byebug result.
[1070, 1079] in /Users/Clayton/.rvm/gems/ruby-2.5.1/gems/actionpack-5.2.0/lib/action_controller/metal/strong_parameters.rb
1070: include ActiveSupport::Rescuable
1071:
1072: # Returns a new ActionController::Parameters object that
1073: # has been instantiated with the request.parameters.
1074: def params
=> 1075: @_params ||= Parameters.new(request.parameters)
1076: end
1077:
1078: # Assigns the given +value+ to the +params+ hash. If +value+
1079: # is a Hash, this will create an ActionController::Parameters

Posted in Sortable Drag and Drop

Hi

I am hoping someone can assist. I have gone through the tutorial of Srttable Drag and Drop.
https://gorails.com/episodes/sortable-drag-and-drop

All is working but once the drop happens I am getting a NoMethodError
NoMethodError (undefined methodeach_with_index' for nil:NilClass):`

This is happening on the sort action in the controller (mine being admin/products)

def sort
    params[:product].each_with_index do |id, index|
        Product.where(id: id).update_all(position: index + 1)
    end

    head :ok
end

Anyone know what could be causing this ?

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.