# Conditional "link\_to" helper function - AYUDAME POR FAVOR

**URL:** <https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511>\
**Category:** Rails\
**Created:** [July 8, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511 "2008-07-08T17:51:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 8, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511/1 "2008-07-08T17:51:57Z")

</div>

Hello,

I need to write a function that will return a link only if the current  
user is the owner. Here is my code…

1. application\_helper.rb
2. 
3. def link\_to\_if\_owned(owner\_id, anchor\_text, where\_to\_go)
4. if current\_user.id == owner\_id # current user is owner
5. 

```
"#{link_to anchor_text, where_to_go}"

```

6. else
7. 

```
anchor

```

8. end
9. end

And here’s how I call it in the view…

1. \<%= link\_to\_if\_owned(@car.owner\_id, “Add Gas”, “:controller =\>  
:cars, :action =\> :add\_gas, :car\_id =\> @car.id”)%\>

The downside is that the actual HTML rendered is a bad link (this is  
what I get)…

1. Add Gas

How can I make it so the link works?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 8, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511/2 "2008-07-08T18:02:47Z")

</div>

On 8 Jul 2008, at 16:51, Joe P. wrote:

> 1. 
> 
> ```
> "#{link_to anchor_text, where_to_go}"
> 
> ```

That’s unnecessary: just link\_to … is enough

> 

don’t pass the link parameters in a string (after all you wouldn’t  
with a ‘normal’ link\_to would you).

Fred

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 8, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511/3 "2008-07-08T20:07:45Z")

</div>

How can I pass it if I don’t know how many parameters there are going to  
be each time? Let’s say I want to use it like this:

\<%= link\_to\_if\_owned(@car.owner\_id, “Add Gas”, “:controller =\>  
:cars, :action =\> :add\_gas, :car\_id =\> @car.id”)%\>

But also like this:

\<%= link\_to\_if\_owned(@car.owner\_id, “List Cars”, “:controller =\>  
:cars, :action =\> :list”)%\>

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 8, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511/4 "2008-07-08T20:21:18Z")

</div>

Thanks, yeah, I tested it right after I wrote my comment. Turns out it  
works already.

Thanks again for the advice.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 8, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/conditional-link-to-helper-function-ayudame-por-favor/142511/5 "2008-07-08T20:12:21Z")

</div>

On 8 Jul 2008, at 19:07, Joe P. wrote:

> \<%= link\_to\_if\_owned(@car.owner\_id, “List Cars”, “:controller =\>  
> :cars, :action =\> :list”)%\>

That doesn’t make any difference.  
:controller =\> :cars, :action =\> :add\_gas, :car\_id =\> @car.id

is only one parameter (they’re collected into a single hash. try it).  
You don’t need to change your helper at all, just how you are calling  
it.

(if you ever need to capture a variable number of arguments, use  
\*args. You don’t need it here though)

Fred
