I know that by default, views in Rails use the template provided in application.html.erb. However, there's one view that I wouldn't like to use the template provided in application.html.erb, but rather write all the HTML in that view itself. Is that possible to do?
5 Answers
At the end of your controller action, add:
render :layout => false
3 Comments
render "tree.html.erb"?render "tree.html.erb", :layout => falsetree, you can skip the "tree.html.erb" part, and just call render :layout => falseFor a specific action:
class SomeController < ApplicationController
def my_custom_action
render layout: false
end
end
1 Comment
render 'tree.html.erb', layout: falseSure, in your action do something like this:
def action
render :layout => false
end
This assumes there are other actions in your controller which do need the layout. Otherwise, I would specify layout false in the controller root.
If you have multiple actions which don't need a layout, I believe you can do
layout false, :only => [ :action1, :action2 ]
1 Comment
You can achieve the same thing using custom layouts.
e.g. For WelcomeController
Create a custom layout file named
welcome.html.erbinapp/views/layout/. Write your layout code there(don't forget theyield). Due to railsConvention over Configurationfeature when rails renders any view mapped toWelcomeController,welcome.html.erbwill override the defaultapplication.html.erblayout.If you want to name your custom layout file differently. Rails allows you to do that as well. Name your layout file as
mylayout.html.erb. InWelcomeController, add the following codeclass WelcomeController < ApplicationController
layout 'mylayout'
....
end
If you want custom layout for only a specific action, then on the last line of action write
render layout: 'mylayout'