0

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.

Beartech
  • 6,173
  • 1
  • 18
  • 41
  • before the Pin creation write `p params[:username]` in order to see what the app knows for this specific param (it will print into the console ) The problem may be coming from Postman.. as I am not too sure about the \n etc .. – Maxence Feb 11 '23 at 23:48
  • Can you post the actual postman input? It looks like you are sending each param inside it's own array. So `params[:username]` is probably returning nil. – Beartech Feb 11 '23 at 23:50
  • Scratch that, I just read it wrong. But I still think it's Postman building some incorrect JSON. – Beartech Feb 11 '23 at 23:55
  • I have never used Postman but usually when you use JS fetch method to pass form from front to Rails backend you have to stringify params such as `body: JSON.stringify(Object.fromEntries(new FormData(form)))`maybe Postman needs to be tweaked a bit – Maxence Feb 12 '23 at 00:00
  • I would also add `p params` to see exactly what params represents. – Beartech Feb 12 '23 at 00:02
  • @Maxence I'm not sure which console it would be printed to. I just have a terminal with the rails server running. added the p params line but only the same output as before is printed. – Connor Cyphers Feb 12 '23 at 00:58
  • when I enter a byebug and type params, it gives me: #nil, "controller"=>"pins", "action"=>"create"} permitted: false> these are the paramters I'm passing in on postman. – Connor Cyphers Feb 12 '23 at 00:59
  • 1
    I actually meant terminal. Your Rails server terminal. In order to identify what you have printed, you can precedes with `p "----"` in order to separate the classic logs to what you actually print. I just want to see if you have a param called username : `params[:username]` because it seems you have a big param named `"{\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}"` instead. With all the backward slashes / escapes ... it is difficult to read – Maxence Feb 12 '23 at 01:08

0 Answers0