我的项目中有以下表格:
我正在尝试创建一个嵌套表单来创建产品型号,用户可以在其中输入名称并检查 Attr Vals 这个新的产品型号将有。
这是我到目前为止所做的:
的 attr.rb 的
class Attr < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: true
has_many :attr_vals
end
的 attr_val.rb 的
class AttrVal < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: { scope: :attr_id }
has_many :product_models, through: :product_model_attr_vals
has_many :products, through: :product_attr_vals
belongs_to :attr
end
的 product_model_attr_val.rb 的
class ProductModelAttrVal < ApplicationRecord
validates :product_model_id, uniqueness: { scope: :attr_val_id }
belongs_to :product_model
belongs_to :attr_val
end
的 product_model.rb 的
class ProductModel < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: true
has_many :products
has_many :product_model_attr_vals
has_many :attr_vals, through: :product_model_attr_vals
has_many :attrs, through: :attr_vals
accepts_nested_attributes_for :product_model_attr_vals
belongs_to :product_category
end
的 product_model / new.html.erb 的
<%= form_for @product_model, url: {action: "create"} do |pm_f| %>
<%= pm_f.text_field :name %><br />
<br />
<ul>
<% Attr.all.each do |attr| %>
<li>
<%= label_tag attr.name %><br />
<ul>
<% attr.attr_vals.each do |attr_val| %>
<li>
<%= pm_f.fields_for :product_model_attr_vals, attr_val, index: attr_val.id do |av_f| %>
<%= av_f.check_box :id, { checked: @product_model.attr_vals.where(id: attr_val.id).count > 0 } %>
<%= label_tag attr_val.name %><br />
<% end %>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
<br/>
<%= pm_f.submit "Create" %>
<% end %>
的 product_model_controller.rb 的
def create
if ProductModel.create(product_model_params)
# do nothing
else
@product_model = ProductModel.new
@attrs = Attr.all # delete me
@categories = ProductCategory.all #delete me
end
render 'new'
end
def new
@product_model = ProductModel.new
@attrs = Attr.all
@categories = ProductCategory.all
end
private
def product_model_params
params.require(:product_model).permit(:name, product_model_attr_vals_attributes: [:id, :name])
end
结果是这样的:
我遇到的问题是由于某些错误而没有创建产品模型,我不知道如何修复它们,因为我是Ruby on Rails的新手,我不知道在哪里寻求帮助
我错过了什么?
Processing by ProductModelsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"f56GFgd99m54gAbhn7iPuLLs8oAqsRZDtK58gqLlW8GP3kXs436ozCBkG+745cDF847gDCcEg==", "product_model"=>{"name"=>"New Product Model Name", "product_model_attr_vals_attributes"=>{"0"=>{"1"=>{"id"=>"1"}}, "1"=>{"2"=>{"id"=>"2"}}, "2"=>{"3"=>{"id"=>"3"}}, "3"=>{"4"=>{"id"=>"4"}}, "4"=>{"5"=>{"id"=>"5"}}, "5"=>{"6"=>{"id"=>"6"}}, "6"=>{"7"=>{"id"=>"7"}}, "7"=>{"8"=>{"id"=>"8"}}, "8"=>{"9"=>{"id"=>"9"}}, "9"=>{"10"=>{"id"=>"10"}}, "10"=>{"11"=>{"id"=>"11"}}}}, "commit"=>"Create"}
Unpermitted parameter: :1
Unpermitted parameter: :2
Unpermitted parameter: :3
Unpermitted parameter: :4
Unpermitted parameter: :5
Unpermitted parameter: :6
Unpermitted parameter: :7
Unpermitted parameter: :8
Unpermitted parameter: :9
Unpermitted parameter: :10
Unpermitted parameter: :11
(0.1ms) BEGIN
ProductModel Exists (0.2ms) SELECT 1 AS one FROM `product_models` WHERE `product_models`.`name` = BINARY 'New Product Model Name' LIMIT 1
ProductModelAttrVal Exists (0.2ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
CACHE ProductModelAttrVal Exists (0.0ms) SELECT 1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1 [["LIMIT", 1]]
(0.1ms) ROLLBACK
Rendering product_models/new.html.erb within layouts/application
Rendered product_models/new.html.erb within layouts/application (1.7ms)
Completed 500 Internal Server Error in 20ms (ActiveRecord: 0.7ms)