2016-07-28 16 views
0

ベンダーモデル、製品モデル、およびベンダー製品モデルがあります。私のベンダーのフォームでは、ネストされたフォームを使用してvendor_id、product_id、およびcopiesという属性を持つvendor_productsを作成しました。新しいベンダーを作成すると、vendor_productも作成されます。しかし、いくつかの理由で、それは店vendor_productsテーブルのVENDOR_IDとのproduct_idをしないだけデータベースにデータを格納しないネストされたフォーム4

ベンダー次のように私の団体であるコピー

保存 - >

 has_many :vendor_products 
     has_many :products, through: :vendor_products 

製品 - >

 has_many :vendor_products 
     has_many :vendors, through: :vendor_products 

vendor_product

/_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

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 

マイベンダーこのように見える

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

これを修正する方法を指摘できますか。私は間違って何をしています。私はレール初心者であることを覚えておいてください。

答えて

1
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" %> 
+0

まあそれは今のVENDOR_IDの人口が、PRODUCT_IDはまだ私のコントローラロジックと間違っている他null.Any事を示して?トリックをしたはい –

+0

チェックこの度 – Mukesh

+0

まあ、私のすべてのフィールドは今人口なっています。ありがとうございました –

0

変更この行:

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

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

私はあなたが言及したものを試してみました。しかし、同じ結果が得られます。 vendor_idとproduct_idが空のvendor_productを作成します。 –

関連する問題