Question

I'm failing a not-null condition in model creation, thanks, I think, to strong parameters removing things from my params hash.

Rspec code:

it "creates a new Provider" do
    expect {
      post :create, {:admin_provider => {:name => "foo bar"}}, valid_session
    }.to change(Provider, :count).by(1)
end

Controller Code:

def create
  @admin_provider = Provider.new(okay_params)

  respond_to do |format|
    ...Render Views
  end
end

def okay_params
  params.permit(:admin_provider).permit(:name)
end

Output:

Admin::ProvidersController POST create with valid params creates a new Provider
 Failure/Error: post :create, {:admin_provider => {:name => "foo bar"}}, valid_session
 ActiveRecord::StatementInvalid:
   PG::NotNullViolation: ERROR:  null value in column "name" violates not-null constraint
   DETAIL:  Failing row contains (225, null, null, null, t, null, null, null, null, null, null, null, null).
   : INSERT INTO "providers" DEFAULT VALUES RETURNING "id"

What happened to my params to strip out the "name" parameter? I've tried it as a symbol and a string. I've tried it as a let block instead of hardcoding. No difference. Every time it seems to be stripping out my params. What am I missing?

Was it helpful?

Solution

Update your okay_params definition as:

# app/controllers/providers_controller.rb

def okay_params
  params.require(:admin_provider).permit(:name)
end

Then your spec:

it "creates a new Provider" do
    expect {
      post :create, {:admin_provider => {:name => "foo bar"}}, valid_session
    }.to change(Provider, :count).by(1)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top