Link_to not work?

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/
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 :slight_smile:

Hi Jamal,

You need to use link_to like this:
<%= link_to “test”, “http://www.rubyonrails.org/” %>

instead of what you are doing:
<% link_to “test”, “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/” %>
<% end
end
%>

or

<%
if @categories
for category in @categories
puts link_to “test”, “http://www.rubyonrails.org/
end
end
%>

HTH,

Mischa


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/
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 :slight_smile:

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 <%=

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/” %>
<% end -%>

Cheers,
Mischa


http://boxroom.rubyforge.org