Chen Kinnrot
Chen Kinnrot

Reputation: 21015

jbuilder view renders with layout

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 ?

Upvotes: 1

Views: 745

Answers (2)

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

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.

Upvotes: 8

Afsanefda
Afsanefda

Reputation: 3339

Check this out :

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

Upvotes: 0

Related Questions