Search code examples
ruby-on-railsruby-on-rails-4inherited-resources

Rails 4 Inherited Resources throwing ConstraintException when posting


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?


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