Forum: Ruby on Rails How switching global layout (application.rthml <or> custom.r

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mathieu C. (Guest)
on 2006-02-02 16:08
(Received via mailing list)
Dears,

My railsapp run using '/layout/application.rhtml' and under some
conditions ( Authenticated user, url parameter) I like to switch
globally to another layout, let's say custom.rhtml

Like :

class AppController < ApplicationController
  before_filter :context_layout

  def index
  end
end

# application.rb
def context_layout
    if <context_condition>
       layout('custom')
    end
end


But layout() call is not available into application.rb, and writing

class AppController < ApplicationController

  layout('custom')

  def index
  end
end

Works,but it's impossible to add here test conditions like @params.

The layout method accept a conditions={} parameters, but I dont figure
out how use it.

Thanks for helping

Mathieu
Adam D. (Guest)
on 2006-02-02 16:30
(Received via mailing list)
what about this

# application.rb
def context_layout
   if <context_condition>
      @session[:layout] = 'newlayout'
   else
      @session[:layout] = 'oldlayout'
  end
end

then in the top of your controllers after your before_filter do:

layout "#{@session[:layout]}"
Mathieu C. (Guest)
on 2006-02-02 17:09
(Received via mailing list)
Thanks Adam

That's look pretty DRY but not works.

> then in the top of your controllers after your before_filter do:
>
> layout "#{@session[:layout]}"
>

Unless I'm a noob , @session or @params are NOT available on the
begining of a controller. (before methods definitions) and give a nil
exception.

Cheers,
Mathieu
James A. (Guest)
on 2006-02-02 17:15
(Received via mailing list)
class ApplicationController

  layout :determine_layout   # symbol indicates it's a method call in
this case...

  # ...and here's the method
  def determine_layout
    if user_is_logged_in?
      "name_of_your_template"
    elsif params[:whatever]
      "name_of_another_template"
    else
       "default_template_name" # default
    end
  end
end

... basically lifted from page 370 of the book.

On 2/2/06, Mathieu C. <removed_email_address@domain.invalid> wrote:
>
>
>
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
* J *
  ~
Mathieu C. (Guest)
on 2006-02-02 17:51
(Received via mailing list)
Thank you James,

>   layout :determine_layout   # symbol indicates it's a method call in
> this case...

This way rocks,
This topic is locked and can not be replied to.