0
我有兩個模型,'產品'與'has_many''belongs_to'和'類別'。產品有一個外鍵'category_id'。並在形式產品/ _form我試圖包括類別字段與選擇選項,顯示類別表中的選項。但是當我點擊提交時,category_id沒有被填充到產品表中。請幫助...外鍵沒有在create-rails中填充表格3
product_controller.rb
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to([:backend, @product], :notice => 'Product was successfully created.') }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
_form.html.erb
<%= f.label :category %><br />
<%= f.select :category_id, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select" %>
型號/ product.rb
class Product < ActiveRecord::Base
attr_accessible :name, :desc, :price, :is_special
belongs_to :category
end
個型號/ category.rb
class Category < ActiveRecord::Base
attr_accessible :name
has_many :products
end
,它像一個魅力工作。感謝很多! – shivbharthur