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

Ruby on rails Rails 4个具有自定义嵌套属性名称的强参数,ruby-on-rails,json,strong-parameters,Ruby On Rails,Json,Strong Parameters,我想更改strong参数中属性的名称,这样它最后就不会有“\u attributes” 我有: params.require(:setting).permit(:recording, :special_settings_attributes => [:orientation]) 我正在用以下方法进行测试: describe "Settings Creation" do context 'new setting s

我想更改strong参数中属性的名称,这样它最后就不会有“\u 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模型。

在您的任何操作调用/使用它之前,只需更改您的
参数即可:

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

我会问为什么?这是表单中嵌套关联的现成rails功能。对于API调用,因此参数更人性化,而且在当前设置中,在服务器端比在调用端更容易进行一些调整。谢谢-只需将其更改为:before_action do params[:setting][:special_settings_attributes]| |=参数[:设置]。删除:特殊设置结束