2

I tried to write render in an action, but layout behavior is different, why?

def show
   # assuming we have a partial in app/views/shared/_panel_show.html.erb
   #render "shared/_panel_show" # have layout
   #render "/shared/_panel_show" # without layout
   #render "shared/panel_show" # Template is missing
   #render :partial => "shared/panel_show" # without layout
   render :partial => "/shared/_panel_show",:layout => "application" # have layout
end

I want to render a partial and follow controller layout.

allenwei
  • 4,047
  • 5
  • 23
  • 26
  • How is it different ? Shouldn't you do : render :partial => "/shared/panel_show",:layout => "application" ? – marcgg Feb 16 '10 at 11:09
  • Yes, I can. But I want to using controller defined layout. I don't want to modify many places when i change global layout. – allenwei Feb 16 '10 at 11:21

2 Answers2

3

The whole point of a partial is that it only renders a part of a view and renders it without any layout.

I would suggest creating a new view (and action in our controller), say shared/full_panel_show, which just renders the partial.

<%= render :partial => 'shared/panel_show' %>

Now in your controller render the new view:

def show
  render :action => 'shared/full_panel_show'
end

Depending on what you are doping with the show view, you could just render the partial from it's view instead.

Mike Sutton
  • 4,191
  • 4
  • 29
  • 42
0

I Have a solution to render a partial with layout in controller

render 'shared/_panel_show', layout: "layouts/application"

just remove the "partial" method then add underscore.

Marcelo Austria
  • 861
  • 8
  • 16