问题:产品型号ID不会进入UserProduct模型。
控制器:
UserProduct创建:
def create
@user_product = UserProduct.new(user_product_params)
@product = Product.find(params[:product_id])
@user_product.product_id = @product.id
.......
形成:
<%= form.collection_check_boxes(:product_id, Product.all, :id, :sku) do |c| %>
<%= c.label class:"form-check-inline" do %>
<%= c.check_box + c.text %>
<% end %>
<% end %>
这显示了产品:前端的sku,但ID没有通过。
输入错误如下:
Couldn't find Product without an ID
楷模:
用户产品:
has_and_belongs_to_many :products
产品:
....
has_and_belongs_to_many :user_products
....
根据我的理解,这不能是嵌套资源,因为UserProduct模型应该从Product模型中选择产品然后附加到UserProducts。所以基本上,应用程序上有一个产品列表,然后用户可以从这些产品中选择进入他们的UserProduct模型。
我的代码有什么问题吗?如何在选择下拉菜单中传递ID?
collection_check_boxes
传递了一系列ID。如果您只需要一个,请尝试使用collection_select
。