Rails 4 个具有自定义嵌套属性名称的强参数

Rails 4 strong parameters with custom nested attributes name

我想更改强参数中的属性名称,使其最后没有“_attributes”。

我有:

params.require(:setting).permit(:recording,
                               :special_settings_attributes => [:orientation])

我正在测试它:

  describe "Settings Creation" do

    context 'new setting success' do
      before do
        a = post :create, format: :json, :setting => {
          :recording => "recorded",
          :special_settings_attributes => [:orientation => "left"]
        }

      end

      it 'creates a new setting' do
        expect(Setting.last.special_settings.last.orientation).to eq("left")
      end
    end
  end

end

我要

params.require(:setting).permit(:recording,
                               :special_settings => [:orientation])

我当然尝试过重命名,但是没有创建 SpecialSetting 模型..

只需通过您的任何操作在 called/used 之前改变您的 params

before_action do
  params[:special_settings_attributes] ||= params.delete :special_settings
end