1
votes

I have a controller that sometimes renders html and sometimes json.

For json I use jbuilder views.

There is a default html layout, that for some unknown reason start to get rendered also for the json view.

I found 2 options that fixes the issue

  1. add layout:false to the render call with the json view
  2. call render partial instead a regular render.

I'm just wondering(cause it didn't rendered the layout a few days ago) Is there a way to tell rails to render the layout only for html request formats ?

2
Why don't you use a simple if ? if condition render json and layout false elsif ...Afsanefda

2 Answers

6
votes

Apparently if you layout file name does not have a .html it'll be used for all request types.. my layout file was x.erb changing it to x.html.erb solves this issue.

0
votes

Check this out :

respond_to do |format|
   format.html { render 'something.html.erb'}
   format.json { render json: @next_level.to_json ,layout: false}
end