Search code examples
ruby-on-railsjbuilder

Rails4 Jbuilder templates not being found


I am trying to setup a simple JSON response using Jbuilder. My controller is as follows:

class Api::V1::JobsController < Api::V1::BaseController
   def show
      @job = Job.find(params[:id])
   end
end

I have a jbuilder template here:

views\api\v1\jobs\show.json.jbuilder

For some reason when loading the page in a browser or Postman the controller is hit, but the jbuilder template is not found. I get a:

Rendered text template (0.5ms) Completed 404 Not Found in 298ms

If I modify the show action on the jobs controller and add a render json: @job I get the desired output, but of course it isn't using the jbuilder template.

Can't figure out why it isn't seeing the jbuilder template!

added some addition details if I go to the .json url

Started GET "/api/v1/jobs/69407.json" for 127.0.0.1 at 2016-01-21 19:16:44 -0500
Processing by Api::V1::JobsController#show as JSON
  Parameters: {"id"=>"69407"}
  Job Load (18.3ms)  SELECT  "jobs".* FROM "jobs" WHERE "jobs"."id" = $1 LIMIT 1  [["id", 69407]]
  Rendered text template (0.5ms)
Completed 404 Not Found in 86ms (Views: 1.1ms | ActiveRecord: 56.4ms)

update - I have found that if I specifically reference the jbuilder template it loads:

render 'show', formats: [:json], handlers: [:jbuilder], status: 200

Also - in order to even get the above to work, I had to add the jbuilder gem to my gem file - which is odd as I am on Rails 4 which should include it by default?


Solution

  • What I think is that you might have to put a respond_to :json in your application controller or base controller. The default is html and I see a rendered text template.