# Question about dynamic layout

**URL:** <https://www.ruby-forum.com/t/question-about-dynamic-layout/103972>\
**Category:** Rails\
**Created:** [May 12, 2007, 4:08am UTC](https://www.ruby-forum.com/t/question-about-dynamic-layout/103972 "2007-05-12T04:08:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 12, 2007, 4:08am UTC](https://www.ruby-forum.com/t/question-about-dynamic-layout/103972/1 "2007-05-12T04:08:52Z")

</div>

Hi,

I have implemented a method where javascript would be found in an  
index file, and depending on the result, a session variable would be  
set to determine the layout the user is to use.

Now heres the problem. Not only does javascript determine the layout,  
but the user can manually force 1 layout versus another. so think of  
this process:

user goes to [mysite.com/index](http://mysite.com/index) (javascript determines which layout to  
use, for this case, lets say it uses layout2)

user then manually goes to [mysite.com/layout1](http://mysite.com/layout1) (layout1 is set in the  
session variable and used)

up to this point, everything is working fine, but imagine if the user  
click back, and then clicks on a link, from the users perspective, it  
would seem like layout2 would be used, since thats the layout that was  
used when the link was clicked on, but since layout1 was the last set  
session variable, it will be used instead.

i want to avoid this problem, so i have been trying to figure out a  
way to pass the layout mode as a params value (i.e. [mysite.com/page](http://mysite.com/page)?  
layout=1). Is this a good idea? is there was another way to do this?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 12, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/question-about-dynamic-layout/103972/2 "2007-05-12T13:45:26Z")

</div>

gmoniey

 \> i want to avoid this problem, so i have been trying to figure out a \> way to pass the layout mode as a params value (i.e. mysite.com/page? \> layout=1). Is this a good idea? is there was another way to do this?

I don’t understand your question but here is my answer :

## in the controller :

…  
layout proc {|controller|  
if controller.action\_name  
conditon = …  
condition ? ‘layout\_1’ : ‘layout\_2’  
else  
‘layout\_3’  
end  
}  
…

## Alain R.

[blog.ravet.com/](http://blog.ravet.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 13, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/question-about-dynamic-layout/103972/3 "2007-05-13T23:38:33Z")

</div>

Hi Alain,

i actually have something like that already, but here is the  
problem…

lets imagine you go to [mysite.com/page1](http://mysite.com/page1) and that page sets the  
condition so that layout1 is used (for all links on that page)…i.e.  
lets say these is an “About” link…when it is clicked, layout1 is  
used…now, lets say the user navigates to [mysite.com/page2](http://mysite.com/page2) and that  
page sets the condition so that layout2 is used.

here is where i feel my design is flawed…if the user is to click  
the back button (i.e. goes back to [mysite.com/page1](http://mysite.com/page1)) and clicks on a  
link, layout2 is going to be used (since that is the last layout the  
condition has been set to)…this is a mistake (in my mind), since i  
think that layout1 should come up, since that is whats expected…

i have been thinkin about how to address this, and it seems that other  
than using different explicit actions to define the layouts…or pass  
the current layout as a parameter (although this will end up with ugly  
URLs)…
