Stpn
Stpn

Reputation: 6394

Rails 4 strong parameters with custom nested attributes name

I want to change the name of the attribute in strong parameter so it does not have "_attributes" in 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

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

I tried renaming of course, but then the SpecialSetting model is no created..

Upvotes: 3

Views: 1138

Answers (1)

smathy
smathy

Reputation: 27961

Just alter your params before it's called/used by any of your actions:

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

Upvotes: 3

Related Questions