Liz
Liz

Reputation: 1457

ActiveSupport::MessageVerifier::InvalidSignature Error with Rails Sortable

I'm using the rails sortable gem to create a table that sorts goodies via drag and drop.

I followed the documentation and got this in my view:

<main class="container-fluid px-5">
  <header class="text-center">
    <h1 class="thin uppercase">Store Manager</h1>
  </header>

  <section>
    <h2 class="color-spouse font-weight-bold">Product Categories (Goodies)</h2>
    <table id="selectedColumn" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
      <thead>
        <tr>
          <th></th>
          <th>Order</th>
          <th>Category</th>
          <th># Free</th>
          <th># Paid</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody class="sortable">
        <% @goodies.each do |g, sortable_id| %>
          <tr id="<%= sortable_id %>">
            <td class="min"><i class="fas fa-grip-lines"></i></td>
            <td class="min"><%= g.sort %></td>
            <td><%= g.name %></td>

            <% free = Variation.where(goody_id: g.id).where(price: 0).count %>
            <td><%= free %></td>

            <% paid = Variation.where(goody_id: g.id).where.not(price: 0).count %>
            <td><%= paid %></td>

            <td class="min">
              <%= link_to '<i class="fas fa-pencil-alt"></i>'.html_safe, edit_goody_path(g), class: "grey-text" %>
              <%= link_to '<i class="fas fa-trash"></i>'.html_safe, g, method: :delete, data: { confirm: 'Are you sure?' }, class: "grey-text" %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </section>

</main>




<%= content_for :additional_js do %>
  <script>
    $(document).ready(function() {
      $(function() {
        $('.sortable').railsSortable();
      });
    })
  </script>
<% end %>

I have this in my controller:

def store_manager
    @goodies = Goody.order(:sort).all
    @variations = Variation.order(:sort).all
  end

And it does drag and drop as expected. HOWEVER, when I do so it comes up with this server error:

Started POST "/sortable/reorder" for ::1 at 2020-05-18 21:13:47 -0700
Processing by SortableController#reorder as JSON
  Parameters: {"rails_sortable"=>["", "", ""], "sortable"=>{"rails_sortable"=>["", "", ""]}}
   (0.2ms)  BEGIN
  ↳ /Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98
   (0.2ms)  ROLLBACK
  ↳ /Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)



ActiveSupport::MessageVerifier::InvalidSignature - ActiveSupport::MessageVerifier::InvalidSignature:

And then when I refresh the page the new order isn't saved.

I can't see what this has to do with activerecord, as most of that is for blobs and file storage. Can anyone see why this is triggering this error?

Upvotes: 2

Views: 2254

Answers (2)

Clemens Kofler
Clemens Kofler

Reputation: 1968

It looks like you're not submitting the IDs to the backend (as you can tell by the fact that the rails_sortable array in the params hash contains blank strings in the log output). All that you're missing seems to be to replace @goodies.each with @goodies.each_with_sortable_id (as the documentation says).

Upvotes: 2

Sandip Mane
Sandip Mane

Reputation: 1633

I faced similar error for the blobs!

In my case, I had a Profile and profile has_one_attached :profile_image.

I got this error when I was making changes to the profile object and trying to get the blob_url for the profile_image.

That's because in backend rails signs every object and when it tried to get the blob_url, the signature was changed as I had called assign_attributes on the same object.

So I had to separate those two API calls. And it solved my problem.

In your case, I guess it's trying to update the columns in the database and fetch the records(which have now changed).

I know, it's not the direct answer but you could find your solution along this line.

Upvotes: 2

Related Questions