I am using rails 5.2.4.1, and I am wondering why I get this error when I try to access the API endpoint:
ActionView::MissingTemplate
(Missing template api/schools/classrooms, application/classrooms with{:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :haml]}
.
here is my action:
def classrooms
render json: {classrooms: user.daip.class_rooms.as_json(:include => [:users]), max_classrooms: user.daip.classrooms} , content_type: 'application/json'
end
I tried also to add default json response to all classrooms_controller
as:
resources :schools, :defaults => { :format => 'json' }
I tried to add .json
to the route but also, did not work
how can I debug this? as it works locally, but not at the production server? I am using nginx with passenger.
What could be the issue?
EDIT
I also tried:
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
EDIT2
I found that header HTTP_ACCEPT
is passed as:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
I set it to application/json, text/plain, */*
, still, rails search for a template!!
EDIT3
I tried to set default request to json as:
request.format = :json
and I tried to use format.json as:
def classrooms
request.format = :json
format.json {
render plain: {classrooms: user.daip.class_rooms.as_json(:include => [:users]), max_classrooms: user.daip.classrooms}.to_json , content_type: 'application/json'
}
end
And I still have the same error, searching for a template..
EDIT4
Here is my request headers:
Host: myapp.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
Referer: https://myapp.com/en/api/school-admin
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
TE: Trailers
Origin: https://myapp.com
Content-Length: 0
and route:
namespace :api, defaults: {format: :json} do
get 'classrooms' => 'schools#classrooms'
end
render json: { results: your_output }, code: :ok
in the last line. I don't know more than that about rails APIs. I hope it can solve your issue. – Navvyrender plain: { hash_content }.to_json, content_type: 'application/json'
. reference: github.com/rails/rails/issues/23923 – Navvyajaxing
lines, and yr routes dnt haveclassrooms
method, or am i missing smth here, nor you have not posted it – Scut