Rails 4 strong parameters with custom nested attribute names

I want to change the name of the attribute in the strong parameter so that it doesn't have "_attributes" at the end.

I have:

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

      

I am testing it with

  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

      

I want to

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

      

I tried to rename, of course, but then the SpecialSetting model is not created.

+3


source to share


1 answer


Just change params

yours before it is called / used by any of your actions:



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

      

+3


source







All Articles