In my app I'm receiving this error.
Couldn't find Vendor with ID=1 for InventoryItem with ID=
InventoryItem.rb
belongs_to :vendor
accepts_nested_attributes_for :vendor
Vendor.rb
has_many :inventory_items
_form.html.erb
<%= simple_nested_form_for @inventory_item, :html => {:class => 'form-inline' } do |f| %>
<h2>Inventory Data</h2>
<%= f.input :name, :input_html => {:autocomplete => :off, :placeholder => 'Item Name' }%>
<%= f.input :id, :as => :hidden %>
<%= f.simple_fields_for :vendor do |v| %>
<%= v.input :name, :label => 'Vendor name', :input_html => {:autocomplete => :off, :placeholder => 'Vendor Name' } %>
<%= v.input :id, :as => :hidden %>
<% end %>
<% end %>
----snip----
My parameters hash comes out accordingly
{"utf8"=>"✓",
"authenticity_token"=>"ZY9fum4XGStTMNbpRQxrzmP7PT3A6BUU+wOymV0fZ/c=",
"inventory_item"=>{"name"=>"testing",
"id"=>"7678",
"vendor_attributes"=>{"name"=>"test",
"id"=>"1"},
"item_instances_attributes"=>{"0"=>{"barcode"=>"",
"funding_source"=>"",
"serial"=>"",
"version"=>"",
"website_id"=>"",
"item_type"=>"Retail",
"type_of_at"=>"Vision",
"os"=>"Mac",
"registration_key"=>"",
"dealer_price"=>"",
"retail_price"=>"",
"reuse_price"=>"",
"estimated_current_purchase_price"=>"",
"cost_to_consumer_for_loan"=>"",
"repair_status"=>"Working",
"date_reviewed"=>"10-15-2012",
"qr_url"=>"",
"location"=>"",
"restrictions"=>"",
"notes"=>""}}},
"commit"=>"Create Inventory item"}
inventory_items_controller.rb
def create
params[:inventory_item].delete(:estimated_dealer_price)
@inventory_item = InventoryItem.create(params[:inventory_item])
@inventory_item.name = inventory_item.name.downcase
if inventory_item.save
redirect_to(inventory_items_path, :notice => "Item created.")
else
render 'new'
end
end
The controller is receiving the id and attempting to find the right vendor (which exists), has issues when left to the built-in rails methods for finding the vendor and building the relationship.
The input for vendor name is an autocomplete which assigns the id to the hidden id field.
possible solutions:
Sounds like you have it in reverse, normally child should not be creating parent records and you should check if its posible to make it in more standard approach of parent child relationship.
That being said you can do something like this
InventoryItem << ActiveRecord::Base
belongs_to :vendor
def vendor_attributes=(params)
self.vendor = Vendor.find(params[:id]) || Vendor.create_by_name!(params[:name])
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.