mercredi 14 août 2019

Using check_box tags to select and pass nested_attributes from another model to db

I have two models Boats & Estimates. Boats contain nested attributes for addons. On the estimate _form view, I have successfully pulled the nested attributes from the boat model and made them check_boxes. However, the checkboxes don't work and the selections are not saved. The idea is to be able to select which addons you want and submit only those options. The Estimate show view will list only the addons that were selected. What is the best way to resolve this issue?

Boat Model:

class Boat < ApplicationRecord
  has_many :addons, inverse_of: :boat
  accepts_nested_attributes_for :addons, reject_if: :all_blank, allow_destroy: true
end

Estimate Model:

class Estimate < ApplicationRecord
  belongs_to :boat
end

Here is the Estimate _form view:

%h4 Additional Options & Accessories
  .addon-list
    - if @estimate.boat.present?
      %table.striped
        - @estimate.boat.addons.all.each do |addon|
          %tr
            %td
              = addon.name
              \- #{number_to_currency(addon.price)}
              %span.float-right
                = check_box_tag :addon_selection, addon.id

Here is the Estimates controller:

def filter_addons_by_boat
  @filtered_addons = Addon.where(boat_id: params[:selected_boat])
end

def show
  @checkedaddons = @estimate.boat.addons.where(params[:addon_selection])
end


Filtered Addons view:

$('.addon-list').html('<div class=".addon-list"><table class="striped"><% @filtered_addons.all.each do |addon| %><tr><td><%= addon.name %> - <%= number_to_currency(addon.price)%><span class="float-right"><%= check_box_tag :addon_selection, addon.id, true %></span></td></tr><% end %></table></div>');


Here is the Estimate show view:

- @checkedaddons.each do |addon|
  %tr
    %td.feature-format
      = addon.name
      %span.float-right
        = number_to_currency(addon.price)

Routes:

 get 'filter_addons_by_boat' => 'estimates#filter_addons_by_boat'

JS:

$(document).on('turbolinks:load', function()  {
  $("select#estimate_boat_id").on("change", function() {
    $.ajax({
      url:  "/filter_addons_by_boat",
      type: "GET",
      data: { selected_boat: $("select#estimate_boat_id").val() }
    });
  });
});

Schema:

  create_table "addons", force: :cascade do |t|
    t.string "name"
    t.string "description"
    t.integer "price"
    t.integer "boat_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["boat_id"], name: "index_addons_on_boat_id"
  end

  create_table "boats", force: :cascade do |t|
    t.string "name"
    t.string "stock"
    t.string "length"
    t.string "year"
    t.string "style"
    t.string "beam"
    t.string "maxhp"
    t.string "weight"
    t.string "passengers"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "estimates", force: :cascade do |t|
    t.date "date"
    t.integer "customer_id"
    t.integer "boat_id"
    t.integer "outboard_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["boat_id"], name: "index_estimates_on_boat_id"
    t.index ["customer_id"], name: "index_estimates_on_customer_id"
    t.index ["outboard_id"], name: "index_estimates_on_outboard_id"
  end


I need to be able to find only the Boat addons selected when creating the estimate so I can do something with them (in this case, find the total price for all selected addons and add them to total overall price)




Aucun commentaire:

Enregistrer un commentaire