# Playing with link\_to helper

**URL:** <https://www.ruby-forum.com/t/playing-with-link-to-helper/88396>\
**Category:** Rails\
**Created:** [March 10, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/playing-with-link-to-helper/88396 "2007-03-10T14:55:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [March 10, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/playing-with-link-to-helper/88396/1 "2007-03-10T14:55:44Z")

</div>

Hi \*,  
someone has suggestions on how to improve this code ? For example I’d  
like my parameteres to be similar to the original link\_to  
helper, :controller =\> :blah, :action =\> :blah and so on …

def link\_to\_current\_class(name, controller, action)  
link\_to name, :controller =\> controller, :action =\> action if  
params[:controller] != controller  
link\_to name, { :controller =\> controller, :action =\> action },  
{ :class =\> ‘current’ }  
end

A better naming is also appreciated 🙂

ngw

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [March 11, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/playing-with-link-to-helper/88396/2 "2007-03-11T16:07:54Z")

</div>

I think something like this should work

def clever\_name\_for\_link\_helper(text, controller, action)  
options = (if params[:controller] == controller) ? {:class =\>  
:current} :  
{}  
link\_to text, {:controller =\> controller, :action =\> action}, options  
end

I had a hard time with the name [obviously the one I used isn’t real]  
because link\_to\_current\_class implies you’re linking to the  
current\_class.  
Perhaps link\_with\_current\_class or mark\_link\_if\_current\_class or  
something?

RSL
