Ruby on rails 编辑页面未保存更改-Cocoon Gem

Ruby on rails 编辑页面未保存更改-Cocoon Gem,ruby-on-rails,ruby,simple-form,cocoon-gem,Ruby On Rails,Ruby,Simple Form,Cocoon Gem,如果我的语言不正确,请原谅,我对Rails和Ruby比较陌生 我正在使用Cocoon从嵌套模型中当前存在的选项列表中进行选择,但我选择的选项永远不会保存。我几乎可以肯定这与我如何对控制器建模有关,但我不确定错误在哪里 我将在这里发布相关代码 模型文件: # product.rb module Almacenear class Product < Almacenear::ApplicationRecord belongs_to :category, optional: true,

如果我的语言不正确,请原谅,我对Rails和Ruby比较陌生

我正在使用Cocoon从嵌套模型中当前存在的选项列表中进行选择,但我选择的选项永远不会保存。我几乎可以肯定这与我如何对控制器建模有关,但我不确定错误在哪里

我将在这里发布相关代码

模型文件:

# product.rb
module Almacenear
  class Product < Almacenear::ApplicationRecord
    belongs_to :category, optional: true, inverse_of: :product
    has_and_belongs_to_many :shops, inverse_of: :product
  end
end

#shop.rb
module Almacenear
  class Shop < Almacenear::ApplicationRecord
    accepts_nested_attributes_for :products, :reject_if => :all_blank
  end
end
i、 e.在将其保存到Almacenear::Shop的产品ID列表之前,它将尝试显示与Almacenear::Shop关联的ID=15的产品。只是想检查一下,如果我将更新重定向到almacenear_grocermenu.shops_path,即与当前登录的杂货商关联的店铺列表,那么不会有错误,也不会更改与该店铺关联的产品列表

我最初还测试了将控制器中的行从products_attributes:[…]更改为:product_ids=>[],也没有保存任何内容。这是我开始使用Cocoon Gem之前使用Simple_Form的关联和复选框时留下的。这种情况下的错误是,它只会忽略所请求的更改

最后,我想指出的是,问题在于保存商店和产品之间的关联。partial addproducts附带另一个partial,用于编辑商店自己的字段,如名称和地址,当对此进行更改时,它们确实会保存到商店的值中


很抱歉这篇文章太长了,我已经尽我所能详细描述了我的过程,这是我的第一篇文章,所以我希望它不要写得太糟糕。任何帮助都将不胜感激

与您商店的产品无关,或者是复制/粘贴错误?协会如何申报?由于您有一个has\u和\u-belies\u-to\u-many关系,因此可能需要显式地创建联接表。您的商店中没有与产品的关系,或者是复制/粘贴错误?协会如何申报?因为您有一个has_和_-belies_-to_-many关系,所以可能需要使联接表显式。
#shops_controller.rb
module Almacenear
  module Grocermenu
    class ShopsController < Almacenear::ApplicationController
      before_action :authenticate_grocer!
      before_action :find_shop, only: %i[edit update show]
      helper  Almacenear::Grocermenu::Engine.routes.url_helpers
      include Almacenear::Grocermenu::Engine.routes.url_helpers


      def update
        if @shop.update(shop_params)
          redirect_to almacenear_grocermenu.shop_path
        else
          render :edit
        end
      end

      protected

      def shop_params
        params.require(:shop).permit(:business_name, :location,
                                     products_attributes: [:id, :name, :category_id,
                                                           :product_type, :description, :measurement_unit,
                                                           :image, :other_id, :ean,
                                                           :image_file_name, :image_content_type, :image_file_size,
                                                           :image_updated_at, :img_url_web, :_destroy])
      end

      def find_shop
        @shop = Almacenear::Shop.find(params[:id])
      end
    end
  end
end
#_addproduct.html.erb
<%= simple_form_for @shop, url: url do |f| %>
  <div id='products'>
    <%= f.simple_fields_for :products do |product| %>
      <%= render 'product_fields', :f => product %>
    <% end %>
    <div class='links'>
      <%= link_to_add_association '+', f, :products, class:"btn btn-success" %>
    </div>
  </div>
  <%= f.button :submit, value: 'Enviar', class: 'btn-success' %>
<% end %>

# _product_fields.html.erb
<div class='nested-fields'>
  <%= f.select :id, Almacenear::Product.all.collect { |p| [ p.name, p.id ] }, { include_blank: true } %>
  <%= link_to_remove_association '-', f , class:"btn btn-danger"%>
</div>

Couldn't find Almacenear::Product with ID=15 for Almacenear::Shop with ID=1