# Conditinal layout based on controller action

**URL:** <https://www.ruby-forum.com/t/conditinal-layout-based-on-controller-action/152889>\
**Category:** Rails\
**Created:** [November 9, 2008, 2:56am UTC](https://www.ruby-forum.com/t/conditinal-layout-based-on-controller-action/152889 "2008-11-09T02:56:31Z")\
**Posts on this page:** 2\
**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:** [November 9, 2008, 2:56am UTC](https://www.ruby-forum.com/t/conditinal-layout-based-on-controller-action/152889/1 "2008-11-09T02:56:31Z")

</div>

I have a very basic controller with all the restful actions.  
What im trying to do i my views is show a “back to links” link in  
every view except the index view.

Im trying to keep it DRY and use some conditional statment in the  
application.html.erb. I cant seem to get it to work. what i tried was  
using the link\_to\_unless helper, but i cant get it to work.

```
<%= link_to_unless({:controller => "links", :action => "index"},

```

“Back to links”, {:action =\> “index” }) %\>

while this is rendering the Back to links text, its not creating a  
link and rendering it in the links controller with any action

any ideas on how i can accomplish this?  
am i using this wrong?

---

<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:** [November 9, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/conditinal-layout-based-on-controller-action/152889/2 "2008-11-09T23:04:42Z")

</div>

Hi Jason,

removed\_email\_address@domain.invalid wrote:

> What im trying to do i my views is show a “back to links”  
> link in every view except the index view.
> 
> Im trying to keep it DRY and use some conditional statment in the  
> application.html.erb.

I think the helper you’re looking for is link\_to\_unless\_current, but I  
see  
no reason your link\_to\_unless won’t work too. It’s just a matter of  
accessing the controller and / or method names responsible for calling  
the  
render. Those are given by controller.controller\_name and  
controller.action\_name and are available to you in any view. The reason  
you’re getting just the text rendered is that that is the spec’d  
behavior  
when the condition is false (which yours is).

I believe (i.e., untested code follows) that your link\_to\_unless would  
work  
with…

\<%= link\_to\_unless(controller.controller\_name == “links” &&  
controller.action\_name == “index”, “Back to links”, {:action =\> “index”  
})  
%\>

OTOH, link\_to\_unless\_current would just be…

\<%= link\_to\_unless\_current(“Back to links”, {:action =\> “index”}) %\>

HTH,  
Bill
