我是新來的rails和正在學習完成學校的庫存系統數據庫項目。Rails嵌套屬性不保存
這裏是我的項目模型:
class Item < ActiveRecord::Base
self.primary_key ='item_id'
validates :item_id, :presence => true
has_one :vendor_item, :dependent => :destroy
has_one :vendor, :through => :vendor_item
accepts_nested_attributes_for :vendor_item
end
這裏是我的項目控制器:
class ItemsController < ApplicationController
def new
@item = Item.new
@all_vendors = Vendor.all
@item_vendor = @item.build_vendor_item
end
def create
@item = Item.new(item_params)
vendor = params[:vendors][:id]
@item_vendor = @item.build_vendor_item(:vendor_id => vendor)
@item_vendor.save
#raise params.inspect
if @item.save
redirect_to @item
else
render 'new'
end
end
def show
@item = Item.find(params[:id])
@item_vendor = @item.vendor_item
end
def index
@items = Item.all
end
def priceUnder500
@priceUnder500 = Item.where("price < ?", 500)
respond_to do |format|
format.html
format.js
end
end
def priceOver500
@priceOver500 = Item.where("price > ?", 500)
respond_to do |format|
format.html
format.js
end
end
def edit
@item = Item.find(params[:id])
@all_vendors = Vendor.all
@vendor_item = @item.vendor_item
end
def update
@item = Item.find(params[:id])
vendor = params[:vendors][:id]
if @item.vendor_item.blank?
@item.build_vendor_item(:vendor_id => vendor)
end
if @item.update(params[:item].permit(:item_id, :name, :category, :description, :reorder_level, :quantity, :price, :vendor_item_attributes => [:vendor_item_id]))
redirect_to items_path
else
render 'edit'
end
end
def destroy
@item = Item.find(params[:id])
@item.destroy
redirect_to items_path
end
private
def item_params
params.require(:item).permit(:item_id, :name, :category, :description, :reorder_level, :quantity, :price, :vendor_item_attributes => [:vendor_item_id])
end
end
而且我_form部分物品:
<%= form_for @item do |f| %>
<% if @item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@item.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :item_id, 'Item Id' %><br>
<%= f.text_field :item_id %>
</p>
<%= fields_for @item_vendor do |vii| %>
<div class= "vendorItemId">
<%= vii.label :vendor_item_id%>
<%= vii.text_field :vendor_item_id%><br>
</div>
<% end %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :category %><br>
<%= f.text_field :category %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :reorder_level %><br>
<%= f.text_field :reorder_level %>
</p>
<p>
<%= f.label :quantity %><br>
<%= f.text_field :quantity %>
</p>
<p>
<%= f.label :price %><br>
<%= f.text_field :price %>
</p>
<h3>Vendors:</h3>
<%= fields_for(@item_vendor) do |ab| %>
<div class= "field">
<%= ab.label "All Vendors:" %><br>
<%= collection_select(:vendors, :id, @all_vendors, :id, :name, {},{:multiple => false})%>
</div>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
vendor_item包含引用ITEM_ID ,vendor_id,還有vendor_item_id的另一個屬性。當程序保存時,item_id和vendor_id保存但vendor_item_id不保存。我嘗試了所有在線來源,但似乎無法獲得保存的一個屬性。我正在使用rails 4.提前感謝您提供的任何幫助。
我的解決方案是否適合您? – Pavan