0

I am new to Rails 7 with not much experience with turbo-stream, stimulus:

I have elements listed in <table> where last column has a form that just contains a checkbox. I want to submit the form on checkbox change, push an alert after submission. Also I have some filters as dropdown at the top of table which I don't want them to be changed/reset after submission.

<div class="card card-flush h-xl-100">
  <div class="card-toolbar">
          <div class="d-flex flex-stack flex-wrap gap-4">
            <div class="d-flex align-items-center fw-bold">
              <div class="text-gray-400 fs-7 me-2">Type</div>
              <select class="form-select form-select-transparent text-graY-800 fs-base lh-1 fw-bold py-0 ps-3 w-auto" data-control="select2" data-hide-search="true" data-dropdown-css-class="w-150px" data-placeholder="Select an option">
                <option></option>
                <option value="all" selected="" data-select2-id="select2-data-9-je8b">Show All</option>
                <option value="seller">Sellers</option>
                <option value="vendor">Vendors</option>
              </select>
            </div>
  </div>
  <div class="table-responsive">
  <table>
    <thead>
      <tr>
        <th class="sorting_disabled" rowspan="1" colspan="1" style="width: 180.1px;">Name</th>
        <th class="text-end sorting_disabled" rowspan="1" colspan="1" style="width: 102.1px;">Type</th>
        <th class="text-end sorting_disabled" rowspan="1" colspan="1" style="width: 25.6667px;">Managed</th>
      </tr>
    </thead>

    <tbody>
      <% @profiles.each_with_index do |profile, indx|  %>
        <% metric = profile.metric%>
        <tr>
          <td><%= profile.name %></td>
          <td><%= profile.type %></td>
          <td>
            <div>
              <%= form_for(metric) do |f| %>
                  <%= f.check_box :is_actively_managed, onchange: "this.form.requestSubmit()" %>
              <% end %>
            </div>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>

</div>

Also metric has_many profiles, so there can be multiple checkboxes that would toggle if the user toggles one of specific metric.

Controller looks like this:

class ProfilesController < ApplicationController

  def update
    profile = Profile.find params[:id]
    respond_to do |format|
      if profile.update(profile_params)
        @notice = {message: "Profile updated successfully", type: "success"}
        format.html { redirect_to accounts_amazon_path}
      else
        error_messages = profile.errors.full_messages.join(", ")
        @notice = {message: "Error updating profile - #{error_messages}", type: "error"}
        format.html { redirect_to accounts_amazon_path}
      end
    end
  end

  def profile_params
    params.require(:profile).permit(:is_actively_managed)
  end
end

Currently it works as intended but resets the filters. I want to update all the checkbox of a particular profile to a new state keeping the filters untouched.

I saw this link https://discuss.hotwired.dev/t/form-submit-with-turbo-streams-response-without-redirect/3290, but I don't want to put my table in partial

0 Answers0