rails 4 中的嵌套表单未将数据存储到数据库中

Nested forms not storing data into database in rails 4

我有一个供应商模型、一个产品模型和一个 vendor_product 模型。在我的供应商表单中,我使用嵌套表单创建了 vendor_products,其属性为 vendor_id、product_id 和 copies.On 创建了一个新的供应商,它还创建了一个 vendor_product.但出于某种原因,它不会将 vendor_id 和 product_id 存储在 vendor_products table 中,而只会存储副本

我的联想如下

供应商 ->

        has_many :vendor_products
        has_many :products, through: :vendor_products

一个产品 ->

        has_many :vendor_products
        has_many :vendors, through: :vendor_products

一个vendor_product

        belongs_to :vendor
        belongs_to :product

Vendor.rb

class Vendor < ActiveRecord::Base

  has_many :vendor_products
  has_many :products, through: :vendor_products
  accepts_nested_attributes_for :vendor_products, :products, 
 :allow_destroy => true

end

我的vendors/_form.html.erb

<%= form_for(@vendor) do |f| %>
<% if @vendor.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@vendor.errors.count, "error") %> prohibited this 
 vendor from being saved:</h2>

  <ul>
  <% @vendor.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
  </div>
 <% end %>

  <div class="field">
   <%= f.label :name %><br>
   <%= f.text_field :name %>
 </div>
    :
    :
    :
 <%= f.fields_for :vendor_products do |vproducts| %>
  <div class="field">
  <%= vproducts.label :product %><br>
    <%= collection_select(:product, :product_ids, Product.all, :id,
    :product_name,
             {:prompt => 'Please select', :multiple => true }) %>
  </div>
  <div class="field">
    <%= vproducts.label :copies %><br>
    <%= vproducts.number_field :copies %>
  </div>
  <% end %>

 <div class="actions">
  <%= f.submit %>
</div>
<% end %>

我的vendors_controller.rb

class VendorsController < ApplicationController
      before_action :set_vendor, only: [:show, :edit, :update, :destroy]
      respond_to :json

      def index
        @vendors = Vendor.all.limit(20)
      end


      def show 
      end 


      def new 
        @vendor = Vendor.new
        @vendor.products.build
        @vendor.vendor_products.build
      end


      def edit
      end


      def create
        @vendor = Vendor.new(vendor_params)      

        respond_to do |format|
         if @vendor.save
            format.html { redirect_to @vendor, notice: 'Vendor was   successfully 
     created.' }
           format.json { render :show, status: :created, location: @vendor    }
         else
           format.html { render :new }
           format.json { render json: @vendor.errors, status: 
           :unprocessable_entity }
         end
       end
      end


     def update
        respond_to do |format|
        if @vendor.update(vendor_params)
           format.html { redirect_to @vendor, notice: 'Vendor was successfully 
      updated.' }
           format.json { render :show, status: :ok, location: @vendor }
        else
          format.html { render :edit }
          format.json { render json: @vendor.errors, status: 
     :unprocessable_entity }
        end
      end
    end



      private

      def set_vendor
        @vendor = Vendor.find(params[:id])
      end

      def vendor_params
         params.require(:vendor).permit(:name, :email, :phone_no, :addressline1, 
      :addressline2, :landmark,
      :city, :state, :country, :pincode, :latitude, :longitude, :status, 
        product_attributes: [:product_id, :product_name, :price ], 
        vendor_products: [:vendor_product_id, :vendor_id, :product_id, 
        :copies])
      end
    end

现在创建了供应商和 VendorProduct,但我的 vendor_product 看起来像这样

    {"id":3,
     "vendor_id":null,
     "product_id":null,
     "copies":4,
    }

任何人都可以指出如何解决这个问题。我究竟做错了什么。请记住我是rails新手

更改此行:

<%= collection_select(:product, :product_ids, Product.all, :id,
    :product_name,
             {:prompt => 'Please select', :multiple => true }) %>
  </div>

<%= collection_select(:product, :product_id, Product.all, :id,
    :product_name,
             {:prompt => 'Please select', :multiple => true }) %>
  </div>
change once vendor_products to vendor_products_attributes and look

在您看来

<%= collection_select(:product, :product_ids, Product.all, :id,
    :product_name,
             {:prompt => 'Please select', :multiple => true }) %>

replace with 

<%= vproducts.select :product_id, options_from_collection_for_select(Product.all, "id", "name"), prompt: "Select something" %>