Question

I have a pretty simple model called Deal with name and description where name is non-nullable.

When I post the following to /api/deals

{"name":"oaeu"}

I get the error

SQLite3::ConstraintException: deals.name may not be NULL: INSERT INTO "deals" ("created_at", "updated_at") VALUES (?, ?)

My Model

enter code here

My Controller

class DealsController < InheritedResources::Base

   protected
   def permitted_params
     params.require(:deal).permit(:name)
   end
end

My Model

class Deal < ActiveRecord::Base
end

I can't figure out what is going on!!!

My Gemfile includes:

gem 'rails', '4.0.2'

and

gem 'inherited_resources'

Any ideas?

Was it helpful?

Solution

Params

Firstly, your strong params are incorrect:

  def permitted_params
     params.permit(deal: [:name])
  end

As mentioned in this blog post, and this github post, you'll get errors unless use the above code!


Saving

As mentioned in the comments, it seems your save process is by-passing your inherited resources controller

It seems you're using an API, so perhaps that it sending straight to the model; either way, you'll have to detail how you're saving the inbound data

OTHER TIPS

It seems you need to overwrite the #resource_params method in your controller.

def resource_params
    [ params.require(:deal).permit(:name) ]
end

See: https://github.com/josevalim/inherited_resources/issues/236 http://blog.josemarluedke.com/posts/inherited-resources-with-rails-4-and-strong-parameters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top