ChrisJ
ChrisJ

Reputation: 2792

Rails responds to angular resource.get with html

Rails 4 is responding to an angular resource.get request with HTML, and I can't figure out why.

The javascript is

$resource("/questions/:id", {id: "@id"}}.get({id: 16});

The server is failing because it can't find the HTML view for that request.

Missing template questions/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

If I point my browser to /querstions/16.json it responds as expected with the JSON.

Is there something missing from the request headers?

If I look at the headers, the request seems to have the right request headers to trigger ruby to respond with json...

Request URL:http://localhost:3000/questions/16
Request Method:GET

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Host:amazing_earth.dev
Pragma:no-cache
Referer:http://localhost:3000/demo
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36

Upvotes: 0

Views: 187

Answers (1)

xiaoboa
xiaoboa

Reputation: 1953

This seems to be an issue of rails for quite long time, check this issue

According to the comments there, this issue has been fix in the newest rails version.

And if you don't want to modify the angularjs header, just change the default format for the api route

get 'questions/:id', to: 'questions#show', defaults: { format: 'json' }

Upvotes: 1

Related Questions