Different controller layout with response_to in Rails

I understand that I can call the render method with layout: 'my_layout' to change the layout in the controller method. However, some of my controller methods have the following so that I can easily display JSON:

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

How to change layout for format.html ? Should I replace format.html with render layout: 'my_layout' ? Is it possible to go to format.html ?

+6
source share
1 answer

By default, Rails will use app/views/layouts/application.html.erb as the layout. If you want to use a different layout, create app/views/layouts/my-new-layout.html.erb , then

In your controller method:

 respond_to do |format| format.html { render layout: 'my-new-layout' } format.json { render json: @cars } end 

Here is the relevant section in the Rails Guide: Layouts and Rendering: Options for Rendering

+9
source

All Articles