# Link\_to not work?

**URL:** <https://www.ruby-forum.com/t/link-to-not-work/87977>\
**Category:** Rails\
**Created:** [March 7, 2007, 9:37am UTC](https://www.ruby-forum.com/t/link-to-not-work/87977 "2007-03-07T09:37:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 7, 2007, 9:37am UTC](https://www.ruby-forum.com/t/link-to-not-work/87977/1 "2007-03-07T09:37:58Z")

</div>

Hello,

I have a problem with link\_to, this is my code

\<%  
if @categories  
for category in @categories  
link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)”  
end  
end  
%\>

But this doesn’t output anything to the screen, but with the below code,  
it output the text inside the block.

\<%  
if @categories  
for category in @categories  
%\>test \<%  
end  
end  
%\>

I’m using link\_to in the wrong way or ???

Hope someone can help me out, and thanks 🙂

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 7, 2007, 10:20am UTC](https://www.ruby-forum.com/t/link-to-not-work/87977/2 "2007-03-07T10:20:50Z")

</div>

Hi Jamal,

You need to use link\_to like this:  
\<%= link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)” %\>

instead of what you are doing:  
\<% link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)” %\>

In your example you could do the following:  
\<%  
if @categories  
for category in @categories %\>  
\<%= link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)” %\>  
\<% end  
end  
%\>

or

\<%  
if @categories  
for category in @categories  
puts link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)”  
end  
end  
%\>

HTH,

Mischa

–  
[http://boxroom.rubyforge.org](http://boxroom.rubyforge.org)

Jamal S. wrote:

> Hello,
> 
> I have a problem with link\_to, this is my code
> 
> \<%  
> if @categories  
> for category in @categories  
> link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)”  
> end  
> end  
> %\>
> 
> But this doesn’t output anything to the screen, but with the below code,  
> it output the text inside the block.
> 
> \<%  
> if @categories  
> for category in @categories  
> %\>test \<%  
> end  
> end  
> %\>
> 
> I’m using link\_to in the wrong way or ???
> 
> Hope someone can help me out, and thanks 🙂

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 7, 2007, 10:40am UTC](https://www.ruby-forum.com/t/link-to-not-work/87977/3 "2007-03-07T10:40:25Z")

</div>

I would like to use the “puts” =\> but it doesn’t output anything? The =  
is working good but I don’t like to open and close %\> all the time if I  
need to use \<%=

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 7, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/link-to-not-work/87977/4 "2007-03-07T13:09:45Z")

</div>

You’re right “puts” doesn’t work.

One suggestion – to make it look more clean – is that you make sure in  
the controller that @categories is not nil (instead of in the view). If  
you do that the code in your view could look like this:

\<% for category in @categories -%\>  
\<%= link\_to “test”, “[http://www.rubyonrails.org/](http://www.rubyonrails.org/)” %\>  
\<% end -%\>

Cheers,  
Mischa

–  
[http://boxroom.rubyforge.org](http://boxroom.rubyforge.org)
