我是Rails的新手,并在此基础上构建了一些东西
http://railscasts.com/episodes/403-dynamic-forms
但我在将数据存储在附加字段中时遇到了问题。。。
我有一个ProductType对象,它有许多ProductField对象。ProductField对象也属于ProductType,Product对象属于ProductType。
因此,可以很容易地通过构造函数ProductType添加新的动态字段,但当我试图通过Product控制器在这些字段中设置数据时,什么也不会发生。
我确信这个问题与使用强参数有关,但修复程序已描述
here
和
here
没有帮助。
产品.rb
class Product < ActiveRecord::Base
belongs_to :product_type
serialize :properties, Hash
end
产品类型.rb
class ProductType < ActiveRecord::Base
has_many :fields, class_name: "ProductField"
accepts_nested_attributes_for :fields, allow_destroy: true
end
产品字段.rb
class ProductField < ActiveRecord::Base
belongs_to :product_type
end
产品_控制器.rb
class ProductsController < ApplicationController
def new
@product = Product.new(product_type_id: params[:product_type_id])
end
def product_params
params.require(:product).permit(:name, :price, :product_type_id, {:properties => []})
end
产品类型控制程序.rb
class ProductTypesController < ApplicationController
def product_type_params
params.require(:product_type).permit(:name, fields_attributes: [:id, :name, :field_type, :required, :product_type_id])
end
在控制台日志中:
不允许的参数:财产
Started PATCH "/products/4" for 127.0.0.1 at 2013-10-04 22:54:59 +0400
Processing by ProductsController#update as HTML
Parameters: {"utf8"=>"â", "authenticity_token"=>"my3ra60OUXexmmguk2eqRetizx3tWPMq04Z2PnODJMQ=", "product"=>{"product_type_id"=>"1", "name"=>"Product1", "properties"=>{"gjfghjf"=>"123", "123"=>[""]}, "price"=>"10"}, "commit"=>"Update Product", "id"=>"4"}
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", "4"]]
Unpermitted parameters: properties
附言:也许有人在看播客时也遇到了类似的问题?