Absolute beginner - simple question!

Hi, just followed this tutorial
http://media.rubyonrails.org/video/rails_take2_with_sound.mov, and loved
it. Now have the skeleton of a blog engine, and starting to customize it
and make it my own. If I show you what code I had before that worked,
and show how I changed it, perhaps you can explain why it no longer
works!

(If there is a more suitable group for beginners, please let me know.)

Before:

Comments

<% for comment in @post.comments %>
<%= comment.name %> says: <%= comment.body %>


<% end %>

After:

Comments

<% for comment in @post.comments %>
<%= if comment.website then %>
<%= comment.name %> says:
<%= comment.body %>
<%= else %>
<%= comment.name %> says: <%= comment.body %>
<%= end %>


<% end %>

I have tried to add a simple conditional to add a link to the persons
name. All these things are in a database called comments.

Any ideas? Must be simple. These are in an rhtml file, hence all the <%=
%> - I might need more of those I reckon, but not sure where!

Thanks for your help!

<%= link_to_if(!comment.website.nil?, h comment.name, comment.website)
%> says: <%= h comment.body %>

This should do the trick. Look at the API* for the usage of link_to_if
and link_to_unless.

I guess you are having problems with the <%= if …%>. The <%= (with
the equal sign) means that the block will be evaluated, converted to
string and added to the view. The <% (without equal sign) means that
the block will be evaluated and nothing is going to be added to the
view. Basically:

<%= h “Hello world!” %> outputs “Hello world!”
<% h “Hello world!” %> outputs “” (nothing).

Also, DON’T forget to escape your strings that come from your models.
It is very easy to do so: add the “h” in front of them. This protects
you against several types of attack.

Felipe
On Dec 21, 11:58 pm, Richard Brashaw <rails-mailing-l…@andreas-

[email protected] wrote:

<%= link_to_if(!comment.website.nil?, h comment.name, comment.website)
%> says: <%= h comment.body %>

This should do the trick. Look at the API* for the usage of link_to_if
and link_to_unless.

I guess you are having problems with the <%= if …%>. The <%= (with
the equal sign) means that the block will be evaluated, converted to
string and added to the view. The <% (without equal sign) means that
the block will be evaluated and nothing is going to be added to the
view. Basically:

<%= h “Hello world!” %> outputs “Hello world!”
<% h “Hello world!” %> outputs “” (nothing).

Also, DON’T forget to escape your strings that come from your models.
It is very easy to do so: add the “h” in front of them. This protects
you against several types of attack.

Felipe
On Dec 21, 11:58�pm, Richard Brashaw <rails-mailing-l…@andreas-

Thanks, but it’s still not working - that looks much simpler than my
idea!

Thanks for explaining the difference between <%= and <%, I hadn’t been
able to find that out! Typing <% in google doesn’t find much!

I’m getting a Action Controller: Exception caught Syntax Error, it
shows:

"Extracted source (around line #8):

5:

Comments


6:
7: <% for comment in @post.comments %>
8: <%= link_to_if(!comment.website.nil?, h comment.name,
comment.website) %> says: <%= h comment.body %>
9:

10: <% end %>
11: "

In the error message, any idea why? Running rails 1.8.5.

Thanks for your help!

[email protected] wrote:

Try removing the “h” from there…

On Dec 22, 12:37 am, Richard Brashaw <rails-mailing-l…@andreas-

Ah, brilliant - still it always shows a link, but now there are no
errors, hopefully I can work that out for myself.

If anyone else ever finds this, I think that using
comment.website.length > 0 as a test might be better than
!comment.website.nil? as that always makes a link.

Thanks for your help!

On 22 Dec 2007, at 13:14, Richard Brashaw wrote:

comment.website.length > 0 as a test might be better than
!comment.website.nil? as that always makes a link.

Yes - the empty string is not nil. You may find blank? appropriate: an
empty string is blank, as is nil

Fred

Try removing the “h” from there…

On Dec 22, 12:37Â am, Richard Brashaw <rails-mailing-l…@andreas-

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs