0

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?

Emad
  • 4,110
  • 5
  • 30
  • 35
  • 1
    Where are you actually saving the Deal object? – S. A. Jan 23 '14 at 02:50
  • inherited resource handle the saving. I don't need to do anything to make it work. I have already done that with an older project on rails 3.x and ruby 1.9.x – Emad Jan 23 '14 at 19:38

2 Answers2

1

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

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • I will try the params.permit syntax you suggested but the saving part, I don't think I understand what you mean. The inherited resource controller should handle all the saving automatically. I use the controller to retrieve deals with no problems. – Emad Jan 23 '14 at 19:39
  • 1
    Your answer is partly right but I will give it to you. The correct syntax is: params.permit(:deal => [:name]) edit your answer if you can :) - thanks – Emad Jan 24 '14 at 04:25
0

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

S. A.
  • 3,714
  • 2
  • 20
  • 31