Customizing layouts for functions


#1

I want to be able to have different layouts in my rails program for
different functions.

For example, when a person first comes to the site the layout will have
a link that says ‘home’, ‘login’, or register on the menu bar.

Once the user logs in however, the layout will change. I want it to show
the links ‘account manager’, ‘logout’, etc.

I tried putting "layout ‘layout-name’ " right after I define a function,
but rails ddin’t like this.

Any suggestions?

Thank you!


#2

You can either use

render :layout => ‘custom_layout’

in each of your actions, or in your controller itself:

class MyController < ApplicationController
layout :choose_layout
def choose_layout
# return the string based on the action in params or whatever
end

end

  • james

On 2/21/06, cranberry removed_email_address@domain.invalid wrote:

but rails ddin’t like this.
http://lists.rubyonrails.org/mailman/listinfo/rails

  • J *
    ~

#3

James A. wrote:

You can either use

render :layout => ‘custom_layout’

in each of your actions, or in your controller itself:

class MyController < ApplicationController
layout :choose_layout
def choose_layout
# return the string based on the action in params or whatever
end

Thank you! that is so easy.


#4

cranberry wrote:

James A. wrote:

You can either use

render :layout => ‘custom_layout’

But I have a few functions in the controller that each need their own
separate layout …

isn’t there a way to specify in the function itself what the layout
should be? I want the layout to be in the function itself, otherwise i
have to go and find all the instances of in my program and add ‘:layout
=>’ to them…

Suggestions?


#5

cranberry wrote:

should be? I want the layout to be in the function itself, otherwise i
have to go and find all the instances of in my program and add ‘:layout
=>’ to them…

Look at James’ post again:

"You can either use

render :layout => ‘custom_layout’

in each of your actions, or in your controller itself…"

In other words, “action” = “function”. (And actually, you should call
them “methods” in
Ruby… although in the rails controller case, we use each method as an
“action”.)

b