Avatar of renderbox
renderbox
Flag 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

Ruby

Avatar of undefined
Last Comment
wesgarrison

8/22/2022 - Mon
wesgarrison

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?
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?
renderbox

ASKER
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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
wesgarrison

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
renderbox

ASKER
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?
wesgarrison


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

Open in new window