# Use link\_to in a controller?

**URL:** <https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201>\
**Category:** Rails\
**Created:** [June 27, 2006, 7:36am UTC](https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201 "2006-06-27T07:36:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 27, 2006, 7:36am UTC](https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201/1 "2006-06-27T07:36:55Z")

</div>

Hello.

I realize that link\_to is a helper that generally only has use in a  
view file, but recently I have come across a situation where it would  
be useful in a controller. I am trying to create a flash[:notice] and  
I want to link to another page. Of course, I could hard code the HTML,  
but for the sake of consistency, I was wondering if there is a way to  
abstract it from within the controller (link\_to is not accessible  
there).

Thanks!

Michael S.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 27, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201/2 "2006-06-27T15:43:42Z")

</div>

But I only want to have the user to have the option of visiting the  
link. I am already using redirect\_to() to go to the user’s account  
homepage, but I am using flash[:notice] to display a message saying  
that the modification was successful and because they made it, they  
can do X by clicking here.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 27, 2006, 9:05am UTC](https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201/3 "2006-06-27T09:05:56Z")

</div>

## Use redirect\_to. [Peak Obsession](http://api.rubyonrails.com/classes/ActionController/Base.html#M000209)

Cheers,  
ioana k&a

> **[white bread and home made chocolate](https://boulangerie.wordpress.com/)**
>
> another meek to eat

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 27, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201/4 "2006-06-27T15:50:32Z")

</div>

Hello Michael,

2006/6/27, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> But I only want to have the user to have the option of visiting the  
> link. I am already using redirect\_to() to go to the user’s account  
> homepage, but I am using flash[:notice] to display a message saying  
> that the modification was successful and because they made it, they  
> can do X by clicking here.

You can use #url\_for which will give you the full URL. Then, you’ll  
have to code the anchor link yourself. Alternatively, use a named  
action which you’ll render as a string, and then copy the resulting  
text to flash[:notice].

Finally, you could Textilize your Flash messages, and use the Flash  
way of generating links in your [:notice] message.

Hope that helps !

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [June 27, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/use-link-to-in-a-controller/96201/5 "2006-06-27T17:05:09Z")

</div>

Hello,

Yep, I suppose url\_for() would work as well. Hadn’t though of that,  
thanks! =)

Michael S.
