Reputation: 63
I'm stumped. I'm getting an error of unknown attribute 'products_part' for Part.
I'm starting with trying to create a new part
that is associated with many different products
through the products_parts
table. Relevant code:
# schema.rb
create_table "products_parts", force: :cascade do |t|
t.integer "product_id"
t.integer "part_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Just in case it's relevant, there is a product_id
column existing on the parts
table from my existing setup that only allowed one part per product. I don't think this would interfere though?
# part.rb
class Part < ApplicationRecord
has_many :products_parts
has_many :products, through: :products_parts
accepts_nested_attributes_for :products_parts, :allow_destroy => true
end
# products_part.rb
class ProductsPart < ApplicationRecord
belongs_to :product
belongs_to :part
end
# product.rb
class Product < ApplicationRecord
has_many :products_parts
has_many :parts, through: :products_parts
accepts_nested_attributes_for :parts, :allow_destroy => true
end
--
# parts_controller.html.erb
class PartsController < ApplicationController
before_action :set_part, only: [:show, :edit, :update, :destroy]
def new
@part = Part.new
@part.uploads.build
@products_parts = @part.products_parts.build
@product = Product.order(brand_name: :asc).order(manufacturer_model_number: :asc)
end
def create
@part = Part.new(part_params)
if @part.save
redirect_to part_path(@part), notice: '// Part was successfully created.'
else
render :new
end
end
private
def part_params
params.require(:part).permit!
end
end
Parameters being passed on submit:
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"f+ObKsfs2QNP6l1MEDGSj6EZERMpHT/7vVKjAumC1aQmeTKdlPJNeSv2IZwNgsVPnKt2Siqi9x5oMmD2Ti8kMQ==",
"part"=>
{"products_part"=>{"product_ids"=>["", "191", "7"]},
"manufacturer_model_number"=>"Screws-HB",
"notes"=>""},
"commit"=>"Create Part"}
-
# _form.html.erb
<%= bootstrap_form_for @part, url: parts_path, method: :post, :html => { :multipart => true } do |f| %>
<%= f.fields_for(@products_parts) do |ff| %>
<%= ff.collection_select(:product_ids, @product, :id, :product_select_with_model,
{:placeholder => "Select Product...", hide_label: true, :selected => @part.products.map(&:id)},
{:multiple => true, :class => 'newPartProductSearch', :style => "width:100%;"}) %>
<% end %>
<%= f.submit "Create Part", :style => "float:right;" %>
<% end %>
I am basing a lot of what I did here off of help from this post. If anybody has any ideas on what direction I can go to start fixing this, it would be appreciated!
Upvotes: 0
Views: 43
Reputation: 5905
Replace the _form
with the following code:
# _form.html.erb
<%= bootstrap_form_for @part, url: parts_path, method: :post, :html => { :multipart => true } do |f| %>
<%= f.collection_select(:product_ids, Product.all, :id, :name,
{include_blank: false, :include_hidden => false, :selected => @part.products.map(&:id)},
{:multiple => true}) %>
<%= f.submit "Create Part", :style => "float:right;" %>
<% end %>
It is not tested. But hope it'll work.
Upvotes: 1