Turn off layout for one of action

Ruby on-RailsLayout

Ruby on-Rails Problem Overview


My situation: View action of ReportsController should render pure html, but not as a file (to view it in browser and save it after). So for rendering I use view template view.html.erb and i neet to turn off any layouts for this action. But in other actions of this controller layouts should stay untouched. Works only turning off for whole controller like this:

ReportsController < ApplicationController
  layout false

But that doing it wrong :( for all the actions I tried to use something like this in action:

def view	  
  @report = Report.new(params[:report])
  unless @report.valid?
    render :action => 'new' and return
  else
    render :layout => false	    
  end   
end

What should I do?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

This should do it,

def view
    ...
    render :layout => false
end

Link to Documentation

Solution 2 - Ruby on-Rails

Try this:

ReportsController < ApplicationController
  layout false
  layout 'application', :except => :view

Solution 3 - Ruby on-Rails

In the respond block, add layout: false.

For example:

respond_to do |format|
  format.html { render :layout => false }
end

Solution 4 - Ruby on-Rails

If you want to get a non-standard template, with no layout you can use:

def non_rest
  render template: 'layouts/something_new', layout: false
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAntiarchitectView Question on Stackoverflow
Solution 1 - Ruby on-RailsDavid OrtizView Answer on Stackoverflow
Solution 2 - Ruby on-RailsmckeedView Answer on Stackoverflow
Solution 3 - Ruby on-RailsArchonicView Answer on Stackoverflow
Solution 4 - Ruby on-RailsMugur 'Bud' ChiricaView Answer on Stackoverflow