Morris
Morris

Reputation: 520

Rails receive json params

I'm developing an application which receives some json parameters to create an object through post method, but I can't receive the parameters, I'm receiving blank parameters.

My method is the following:

def create
render :json => params[:name]
end

and my json example is the following:

{"name":"foo"}

but the problem is that it's rendering 'null'

What am I doing wrong?

Thanks in advance!

Upvotes: 1

Views: 477

Answers (1)

Ojash
Ojash

Reputation: 1234

For receiving JSON in the create method, you need to keep few things in mind.

Your request should include the following header

Content-Type: application/JSON

By the look of your question, you are building a API, so you have to change the protect_from_forgery with in your application_controller.rb

protect_from_forgery with: :null_session

Upvotes: 1

Related Questions