Forum: Ruby on Rails Conditinal layout based on controller action.

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.
(Guest)
on 2008-11-09 03:56
(Received via mailing list)
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?
Bill W. (Guest)
on 2008-11-10 00:04
(Received via mailing list)
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
This topic is locked and can not be replied to.