37
else
  respond_to do |format|
    format.html { render "tabelle/show" }
  end
end    

I want to render the page ...with only the code in that page....not add <head>...layout and <body> field in ruby on rails. I only want to show the result of code in the page tabelle/show.html.haml

Stuart M
  • 11,458
  • 6
  • 45
  • 59
mewosic
  • 465
  • 2
  • 7
  • 11

4 Answers4

44

You can do it like this:

format.html { render "tabelle/show", :layout => false  } 
fmendez
  • 7,250
  • 5
  • 36
  • 35
  • You can also do this controller wide by calling `layout false` on the controller: class FooController < ApplicationController; layout false; end – Martin Svalin Apr 11 '13 at 13:12
  • @MartinSvalin You can indeed do that, but that affects all the actions within the `FooController`, it seems the OP have a more localised need for controlling the layout. – fmendez Apr 11 '13 at 13:19
  • 2
    Can you render without specifying the view to use, like `render layout: false` and it will use the `show.html.erb` in the `tabelle` directory automatically but without the typical layout? – Joshua Pinter Jan 29 '18 at 23:28
  • 1
    Turns out you can! – Joshua Pinter Jan 29 '18 at 23:29
13

Controller:

layout false, only: [:method_name]

this is very useful when you using render_to_string

Fabricioblz
  • 161
  • 1
  • 4
9

add

:layout => false

Example:

render "tabelle/show", :layout => false
HungryCoder
  • 7,506
  • 1
  • 38
  • 51
  • What if i render a partial,and i want to show the layout...i render it directly from the controller, i do 'render partial:'successful_download', locals:{link:@gig.boxlink}' and it does work,but the layout disappears.How to make it appear? – Mike McCallen May 17 '15 at 14:17
9

If you do not want to specify the view to use.

Rails is smart enough to know which view template to use based on the Controller Action you're on.

For example, if you're on the show action of the TabellesController you wouldn't need to specify render "tabelle/show" in your Controller Action because Rails will already assume that and will automatically try to render the file in app/views/tabelles/show.html.erb.

So if you're sticking with all of those defaults then you can just use the following to render without the typical layout template:

def show
  # Other stuff in your Controller Action.

  render layout: false
end

This will render app/views/tabelles/show.html.erb but without the layout template automatically.

Noice.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245