我按照步骤http://railscasts.com/episodes/197-nested-model-form-part-2进行了更改并更改了purchase_product"调查"和supplier_product as"问题"但是没有保存,也没有保存嵌套属性。
这是控制器/app/controller/purchase_product_controller.rb
class PurchaseProductController < ApplicationController
def new
@purchase = PurchaseProduct.new
1.times do
supplier_product = @purchase.supplier_products.build
end
end
def create
@purchase = PurchaseProduct.new(params[:purchase])
if @purchase.save
flash[:notice] = "Successfully created purchase."
redirect_to :action=>"index"
else
render :action => 'new'
end
end
end
这里的型号:
class PurchaseProduct < ActiveRecord::Base
has_many :supplier_products
accepts_nested_attributes_for :supplier_products ,:allow_destroy => true
end
class SupplierProduct < ActiveRecord::Base
belongs_to :purchase_product
end
这是我的路线:/config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.root :controller => "purchase_product", :action=>"index"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
以下是视图:/app/view/purchase_product/new.html.erb
<% form_for @purchase, :url => {:controller=>"purchase_product",:action=>'create'}, :html => {:id => 'new_product_form'} do |f| %>
Name: <%= f.text_field :name %>
<% f.fields_for :supplier_products do |builder| %>
<%= render "supplier_product_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Supplier Product", f, :supplier_products %></p>
<p><%= f.submit "Submit" %></p>
<% end %>
以下是部分视图:/app/view/purchase_product/_supplier_product_fields.html.erb
<div class="fields">
Type Money: <%= f.select(:type_money,([["%", 0], ["$", 1] ]) ) %>
Cost: <%= f.text_field :amount %><%= link_to_remove_fields "remove", f %>
</div>
但是没有保存并得到了这个日志:
Processing PurchaseProductController#create (for 127.0.0.1 at 2014-08-06 13:48:31) [POST]
Parameters: {"purchase_product"=>{"name"=>"testing", "supplier_products_attributes"=>{"0"=>{"amount"=>"333", "type_money"=>"0", "_destroy"=>""}}}, "commit"=>"Submit"}
PurchaseProduct Columns (0.6ms) SHOW FIELDS FROM `purchase_products`
SQL (0.1ms) BEGIN
PurchaseProduct Create (0.0ms) Mysql::Error: Column 'name' cannot be null: INSERT INTO `purchase_products` (`name`, `created_at`, `updated_at`) VALUES(NULL, '2014-08-06 18:48:31', '2014-08-06 18:48:31')
SQL (0.1ms) ROLLBACK
ActiveRecord::StatementInvalid (Mysql::Error: Column 'name' cannot be null: INSERT INTO `purchase_products` (`name`, `created_at`, `updated_at`) VALUES(NULL, '2014-08-06 18:48:31', '2014-08-06 18:48:31')):
我解决了保存问题,在视图中更改此参数并保存但未保存其他属性
Name: <%= text_field_tag "name",@name,:name=>"purchase_product[name]" %>
我收到了这个日志:
Processing PurchaseProductController#create (for 127.0.0.1 at 2014-08-06 14:00:04) [POST]
Parameters: {"purchase_product"=>{"name"=>"TESTING", "supplier_products_attributes"=>{"0"=>{"amount"=>"100", "type_money"=>"0", "_destroy"=>""}}}, "commit"=>"Submit"}
PurchaseProduct Columns (0.7ms) SHOW FIELDS FROM `purchase_products`
SQL (0.1ms) BEGIN
PurchaseProduct Create (0.3ms) INSERT INTO `purchase_products` (`name`, `created_at`, `updated_at`) VALUES('TESTING', '2014-08-06 19:00:04', '2014-08-06 19:00:04')
SQL (37.0ms) COMMIT
Redirected to http://localhost:3000/
Completed in 44ms (DB: 38) | 302 Found [http://localhost/purchase_product/create]
我花了很长时间搜索解决方案,我也多次重新创建项目。
供应商产品的属性未保存。
请有人帮我解决这个问题吗?
答案 0 :(得分:2)
在你的控制器中,你从params散列中获取purchase
键。
@purchase = PurchaseProduct.new(params[:purchase])
但您需要的参数实际上是params[:purchase_product]
。
@purchase = PurchaseProduct.new(params[:purchase_product])
当您使用form_for
帮助程序时,密钥将以构建表单的模型命名。在您的情况下,它是PurchaseProduct
,因此他们为什么会在params[:purchase_product]