Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Set Layout Based on URL Params

So I am trying to change the layout of a view based on url params.

So far, I figured out I have to set the layout in the controller. In my controller under the show action I have:

if params['iframe'] == 'true'
  render :layout => 'vendored'
end

The layout 'vendored' exists in views/layouts. I am getting the dreaded rendering multiple times. Here is the rest of the show action in my controller:

 def show
    @event = Event.find(params[:id])
    @user = current_user
    @approved_employers = current_user.get_employers_approving_event(@event) if user_signed_in?
    respond_with(@event)

The problem is that I don't see another render. I don't see another one in the entire controller. Of course, there is a render somewhere because it is rendering my default application layout, is that causing the problem? I read in the rails docs that I can add

and return

to the end and that should fix the problem, but not sure where to put that since the two renders are not next to each other. I also don't see any other redirect_to's either. Where should I be looking for this other render? Is that the problem?

like image 616
Kevin Z Avatar asked Dec 20 '22 23:12

Kevin Z


2 Answers

Alternatively, I think this is easier to understand:

class YourController < ApplicationController
  layout :iframe_layout

  private

  def iframe_layout
    params['iframe'] ? "vendored" : "application"
  end
end
like image 70
Jonathan Bender Avatar answered Dec 28 '22 07:12

Jonathan Bender


See this answer. For your case:

  before_filter :set_layout, :only => [:show]

  private

  def set_layout
   self.class.layout ( params['iframe'] == 'true' ? 'vendored' :  'application')
  end
like image 30
tihom Avatar answered Dec 28 '22 06:12

tihom