【发布时间】:2016-02-13 00:29:31
【问题描述】:
我有一个名为TaxCategory 的模型,即has_many :tax_rates 和accepts_nested_attributes_for :tax_rates, reject_if: :all_blank, allow_destroy: true。
TaxRates 本身就是一个模型,其中包括has_and_belongs_to_many :countries。
这些关系运行良好,我可以通过控制台添加和删除国家/地区。
但是,我有一个包含 fields_for :tax_rates do |g| 的 TaxCategory 表单。
在里面,我有一个
g.select :country_ids, Country.collect{|c| [c.name, c.id]}, {multiple: true}, {}
它提交给tax_categories控制器,它使用以下代码更新TaxCategory:
class TaxCategoriesController
before_action :set_tax_category, only: [:show, :edit, :update, :destroy]
...*snip*...
def update
respond_to do |format|
if @tax_category.update(tax_category_params)
format.html { redirect_to [:dashboard, @tax_category], notice: 'Tax Category was successfully updated.' }
format.json { render :show, status: :ok, location: @tax_category }
else
format.html { render :edit }
format.json { render json: @tax_category.errors, status: :unprocessable_entity }
end
end
private
def set_tax_category
@tax_category = TaxCategory.find(params[:id])
end
def tax_category_params
params.require(:tax_category).permit(:name, tax_rates_attributes:[:id, :rate,{country_ids: []}, :_destroy])
end
end
但是,这不起作用;提交表单时,只保存第一个 Country,Rails 命令行显示 Unpermitted parameter: country_ids 消息。
我认为这是由params.permit引起的问题,但我不明白我做错了什么。
出了什么问题,我该如何解决?
【问题讨论】:
标签: ruby-on-rails nested-forms nested-attributes strong-parameters