Rails 4强参数与自定义嵌套属性名称

时间:2015-05-28 20:30:54

标签: ruby-on-rails json strong-parameters

我想在strong参数中更改属性的名称,因此它最终没有“_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模型..

1 个答案:

答案 0 :(得分:3)

在您的任何操作调用/使用之前,只需更改params

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