Conditinal layout based on controller action.

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?

Hi Jason,

jason@jasoncalleiro.com 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