【发布时间】:2018-05-20 16:09:55
【问题描述】:
在我的product_model_controller.rb 中,强参数有以下代码:
def product_model_params
params.require(:product_model)
.permit(:name, :product_category_id,
product_category_attributes: [:id, :name], attr_val_ids: [])
end
就目前而言,它工作正常。但是,如果我更改参数的顺序,它就会停止工作。示例:
def product_model_params
params.require(:product_model)
.permit(:name, product_category_attributes: [:id, :name],
:product_category_id, attr_val_ids: [])
end
错误:
语法错误,意外 ',',期待 => ..., :name], :product_category_id, attr_val_ids: []) ... ^
为什么会这样?我已经坚持了很长时间了:/
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
belongs_to :product_category
accepts_nested_attributes_for :product_model_attr_vals
accepts_nested_attributes_for :product_category
end
product_category.rb
class ProductCategory < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: true
has_many :product_models
end
【问题讨论】:
标签: ruby-on-rails forms nested-forms strong-parameters nested-form-for