Nested forms not storing data into database in rails 4 -
i have vendor model, product model, , vendor_product model. in vendors form, have used nested form create vendor_products attributes vendor_id, product_id , copies.on creating new vendor, creates vendor_product. reason, not stores vendor_id , product_id in vendor_products table stores copies
my associations follows
a vendor ->
has_many :vendor_products has_many :products, through: :vendor_products
a product ->
has_many :vendor_products has_many :vendors, through: :vendor_products
a 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
my vendors/_form.html.erb
<%= form_for(@vendor) |f| %> <% if @vendor.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@vendor.errors.count, "error") %> prohibited vendor being saved:</h2> <ul> <% @vendor.errors.full_messages.each |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 |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 %>
my 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 |format| if @vendor.save format.html { redirect_to @vendor, notice: 'vendor 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 |format| if @vendor.update(vendor_params) format.html { redirect_to @vendor, notice: 'vendor 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
now vendor , vendorproduct created vendor_product looks lik this
{"id":3, "vendor_id":null, "product_id":null, "copies":4, }
can 1 point out how fix this. doing wrong. please bear in mind rails newbie.
change once vendor_products vendor_products_attributes ,
in view
<%= collection_select(:product, :product_ids, product.all, :id, :product_name, {:prompt => 'please select', :multiple => true }) %> replace <%= vproducts.select :product_id, options_from_collection_for_select(product.all, "id", "name"), prompt: "select something" %>
Comments
Post a Comment