New entry is created with all values as nil in Rails API

62 views Asked by At

I am using Postman to make a Post request to my Rails backend and the new entry is created successfully with all values being 'nil'.

I can see that the parameters are being passed into the request as expected but I cannot figure out why they aren't making it into the new entry.

My controller:

class PinsController < ApplicationController
    
    def index
        pins = Pin.all
        render json: pins, status: :ok
    end

    def create
        # pin = Pin.create!(pin_params)
        pin = Pin.create(username: params[:username], title: params[:title], desc: params[:desc], rating:    params[:rating], lat: params[:lat], long: params[:long])
        render json: pin, status: :created
    end

    private

    def pin_params
        params.permit(:username, :title, :desc, :rating, :lat, :long)
    end
end

The output of the rails server:

Started POST "/pins" for 127.0.0.1 at 2023-02-11 18:25:40 -0500
Processing by PinsController#create as \*/\* 
Parameters: {"{\\r\\n    \\"username\\": \\"John\\",\\r\\n
\\"title\\": \\"DC\\",\\r\\n    \\"desc\\": \\"not bad\\",\\r\\n
\\"rating\\": \\"4\\",\\r\\n    \\"lat\\": \\"37.689\\",\\r\\n
\\"long\\": \\"41.987\\"\\r\\n}"=\>nil}
Can't verify CSRF token authenticity.
TRANSACTION (0.0ms)  begin transaction
↳ app/controllers/pins_controller.rb:10:in \`create' 
Pin Create (0.2ms)  INSERT INTO "pins" ("username", "title", "desc",
"rating", "lat", "long", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  \[\["username", nil\], \["title", nil\], \["desc", nil\], \["rating", nil\], \["lat", nil\], \["long", nil\], \["created_at", "2023-02-11 23:25:40.308563"\], \["updated_at", "2023-02-11 23:25:40.308563"\]\] 
↳ app/controllers/pins_controller.rb:10:in \`create'
TRANSACTION (4.6ms)  commit transaction 
↳ app/controllers/pins_controller.rb:10:in \`create' 
Completed 201 Created in 13ms (Views: 0.4ms | ActiveRecord: 5.3ms | Allocations: 4648) 

I can't figure out where I am going wrong, and why the parameters are being passed but the values are nil.

0

There are 0 answers