I am trying to create a JSON API for my Rails application and wrote the following method:
def create
organization = Organization.find(params[:organization][:node_id])
node = organization.nodes.build(nodes_params.except[:id])
if node.save
render json: node, status: :ok
else
render json: node, status: :bad_request
end
end
An attempt by a method in Postman returns an error: "Unable to verify the authenticity of the CSRF token." Based on this post , I added the code below to the base controller. Unfortunately, that didn't matter. Does anyone understand the cause of the error?
protect_from_forgery
skip_before_action :verify_authenticity_token, if: :json_request?
private
def json_request?
request.format.json?
end
source
share