I have a PostResource with title, upvotes and active as attributes. I'm making POST api call with the data as such
{"post":{
"title": "example1234",
"upvotes": 67,
"active": true
}
}
But the values for title, upvotes and active are always saves as NULL in the database. Can you help me solve this.
Here is my controller create action
def create
post_params = params.require(:post).permit(:title, :upvotes, :active)
@post = PostResource.build(post_params)
byebug
if @post.save
render jsonapi: @post, status: 201
else
render jsonapi_errors: @post
end
end
And here is output from debugger
(byebug) post_params <ActionController::Parameters {"title"=>"example1234", "upvotes"=>67, "active"=>true} permitted: true> (byebug) @post Post Load (1.3ms) SELECT "posts".* FROM "posts" LIMIT ? OFFSET ? [["LIMIT", 11], ["OFFSET", 0]] ↳ app/controllers/api/v1/posts_controller.rb:16:in
create' Post Load (1.0ms) SELECT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] ↳ app/controllers/api/v1/posts_controller.rb:16:in create' #<Graphiti::ResourceProxy:0x00007f3c38c11378 @resource=#<PostResource:0x00007f3c38c05af0 @adapter=#<Graphiti::Adapters::ActiveRecord:0x00007f3c38c04e70 @resource=#<PostResource:0x00007f3c38c05af0 ...>>>, @scope=#<Graphiti::Scope:0x00007f3c38c13b50 @object=#<ActiveRecord::Relation [#<Post id: 1, title: "My title", upvotes: 10, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 2, title: "Another title", upvotes: 20, active: false, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 3, title: "OMG! A title", upvotes: 30, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 4, title: "Dynamo", upvotes: 10, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 5, title: "Panda", upvotes: 20, active: false, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 6, title: "Mortal", upvotes: 30, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 7, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:11:05", updated_at: "2020-03-03 09:11:05">, #<Post id: 8, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:13:07", updated_at: "2020-03-03 09:13:07">, #<Post id: 9, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:16:35", updated_at: "2020-03-03 09:16:35">, #<Post id: 10, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:21:10", updated_at: "2020-03-03 09:21:10">, ...]>, @resource=#<PostResource:0x00007f3c38c05af0 @adapter=#<Graphiti::Adapters::ActiveRecord:0x00007f3c38c04e70 @resource=#<PostResource:0x00007f3c38c05af0 ...>>>, @query=#<Graphiti::Query:0x00007f3c38c041c8 @resource=#<PostResource:0x00007f3c38c05af0 @adapter=#<Graphiti::Adapters::ActiveRecord:0x00007f3c38c04e70 @resource=#<PostResource:0x00007f3c38c05af0 ...>>>, @association_name=nil, @params={:title=>"example1234", :upvotes=>67, :active=>true}, @include_param=nil, @parents=[], @filters={}, @pagination={}, @fields={}, @extra_fields={}, @stats={}, @include_directive=#<JSONAPI::IncludeDirective:0x00007f3c38c138f8 @hash={}, @options={}>, @include_hash={}, @sideloads={}, @sideload_hash={}, @hash={}>, @opts={}, @unpaginated_object=#<ActiveRecord::Relation [#<Post id: 1, title: "My title", upvotes: 10, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 2, title: "Another title", upvotes: 20, active: false, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 3, title: "OMG! A title", upvotes: 30, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 4, title: "Dynamo", upvotes: 10, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 5, title: "Panda", upvotes: 20, active: false, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 6, title: "Mortal", upvotes: 30, active: true, created_at: "2020-03-02 12:44:55", updated_at: "2020-03-02 12:44:55">, #<Post id: 7, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:11:05", updated_at: "2020-03-03 09:11:05">, #<Post id: 8, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:13:07", updated_at: "2020-03-03 09:13:07">, #<Post id: 9, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:16:35", updated_at: "2020-03-03 09:16:35">, #<Post id: 10, title: nil, upvotes: nil, active: nil, created_at: "2020-03-03 09:21:10", updated_at: "2020-03-03 09:21:10">, ...]>>, @query=#<Graphiti::Query:0x00007f3c38c041c8 @resource=#<PostResource:0x00007f3c38c05af0 @adapter=#<Graphiti::Adapters::ActiveRecord:0x00007f3c38c04e70 @resource=#<PostResource:0x00007f3c38c05af0 ...>>>, @association_name=nil, @params={:title=>"example1234", :upvotes=>67, :active=>true}, @include_param=nil, @parents=[], @filters={}, @pagination={}, @fields={}, @extra_fields={}, @stats={}, @include_directive=#<JSONAPI::IncludeDirective:0x00007f3c38c138f8 @hash={}, @options={}>, @include_hash={}, @sideloads={}, @sideload_hash={}, @hash={}>, @payload=#<Graphiti::Deserializer:0x00007f3c38c05410 @payload={}, @attributes={}, @relationships={}>, @single=true, @raise_on_missing=true> (byebug) c (0.2ms) begin transaction ↳ app/controllers/api/v1/posts_controller.rb:16:in
create' Post Create (2.1ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2020-03-04 12:26:49.819298"], ["updated_at", "2020-03-04 12:26:49.819298"]] ↳ app/controllers/api/v1/posts_controller.rb:16:in create' (21.1ms) commit transaction ↳ app/controllers/api/v1/posts_controller.rb:16:in
create' Completed 201 Created in 43800ms (Views: 1.4ms | ActiveRecord: 27.5ms | Allocations: 54753) `