Link to home
Create AccountLog in
Avatar of renderbox
renderboxFlag for United States of America

asked on

Controller condition with link_to_unless

I am trying to use link_to_unless to control the style tags on a link.  I have also mapped the URL to "www.somesite.com/fred".  What I am trying to get to work is that I want the condition to be based on the controller.  I want the result to be the same as long as any actions are within the controller.

It's a tab based interface with subtabs.  When a user clicks on a subtab I don't want the main tab to loose the highlighting from the style.  Since the tab and sub tabs actually map to controllers and actions this should be possible.  Below is the code I am trying to use for the main tabs but I can't seem to get the condition to work right.  In this case I wan the tab to be active when some goes to "www.somesite.com/fred" or "www.somesite.com/fred/page2" not when they go to "www.somesite.com/bob".
<%= link_to_unless( params[:controller]  == "fred", "Fred", fred_url, :class => "tab") do
      link_to("Fred", fred_url, :class => "tab-active")
   end %>

Open in new window

Avatar of wesgarrison
wesgarrison
Flag of United States of America image

So, where's your problem?  First glance, it looks like everything is in the right spot.  

Is it just backwards?  What's not working correctly for you?
Avatar of renderbox

ASKER

When i tested this out I found the problem I am running into is that I am picking up the name of the "real" controller and not the one it is mapped to.

I'd like to the result to be one result from http://mysite.com/fred and http://mysite.com/fred/new and the other result from http://mysite.com/bob of http://mysite.com/foo.  The problem I am noticing is that since fred_url, bob_url and foo_url use the same real :controller I don't get the result I am looking for.

This seems to be my misunderstanding of how to get the mapped URL instead of the real URL to check against.  How would I make the check on a mapped URL?
Here is the improved version of my question.

In "http://mysite.com/XXXXX" how would I query XXXXX to see what it's value is when http://mysite.com/XXXXX is a mapped URL?
ASKER CERTIFIED SOLUTION
Avatar of wesgarrison
wesgarrison
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This seems to work.

I did this as the test:

( request.path_info[1,6]  == "XXXXX" )

I imagine there is a better way to do this.  In Python I would use the 'startswith()' method.  Is there something like that in RoR without regex?

request.path_info.split(\.\)   # to split on periods?
# => ['www', 'domain', 'com']

Open in new window