Search code examples
ruby-on-railsjsonruby-on-rails-3.1jbuilder

Rails 3.1: Jbuilder rendering empty views


Trying to use Jbuilder to create some JSON views for my app, but the views are coming out empty. Using the builder code in the console, however, works just fine.

Controller code:

@placements = Placement.all
respond_to do |format|
  format.html
  format.json
end

Jbuilder view (index.json.builder):

Jbuilder.encode do |json|
  json.array!(@placements) do |json, placement|
    json.id placement.id
    json.name placement.name
  end
end

Visiting http://localhost:3000/placements.json results in an empty page. Removing the respond_to format block doesn't help. If I use the following code in my controller, I get an output, but it's obviously not the Jbuilder output.

respond_to do |format|
  format.html
  format.json {render json: @placements}
end

Has anyone else seen this problem?


Solution

  • @Robin - It was rendering the wrong template, but I had the wrong extension. I was using builder instead of jbuilder.