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: <h2>Comments</h2> <% for comment in @post.comments %> <%= comment.name %> says: <%= comment.body %> <hr /> <% end %> After: <h2>Comments</h2> <% for comment in @post.comments %> <%= if comment.website then %> <a href="http://<%= comment.website %>"><%= comment.name %></a> says: <%= comment.body %> <%= else %> <%= comment.name %> says: <%= comment.body %> <%= end %> <hr /> <% 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-22 08:27 UTC
Re: Absolute beginner - simple question!
<%= 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. * http://api.rubyonrails.org/ Felipe On Dec 21, 11:58 pm, Richard Brashaw <rails-mailing-l...@andreas- s.net> wrote:> Hi, just followed this tutorialhttp://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: > > <h2>Comments</h2> > > <% for comment in @post.comments %> > <%= comment.name %> says: <%= comment.body %> > <hr /> > <% end %> > > After: > > <h2>Comments</h2> > > <% for comment in @post.comments %> > <%= if comment.website then %> > <a href="http://<%= comment.website %>"><%= comment.name %></a> says: > <%= comment.body %> > <%= else %> > <%= comment.name %> says: <%= comment.body %> > <%= end %> > <hr /> > <% 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! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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. > > * http://api.rubyonrails.org/ > > 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: <h2>Comments</h2> 6: 7: <% for comment in @post.comments %> 8: <%= link_to_if(!comment.website.nil?, h comment.name, comment.website) %> says: <%= h comment.body %> 9: <hr /> 10: <% end %> 11: " In the error message, any idea why? Running rails 1.8.5. Thanks for your help! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-22 08:49 UTC
Re: Absolute beginner - simple question!
Try removing the "h" from there... On Dec 22, 12:37 am, Richard Brashaw <rails-mailing-l...@andreas- s.net> wrote:> felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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. > > > *http://api.rubyonrails.org/ > > > 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: <h2>Comments</h2> > 6: > 7: <% for comment in @post.comments %> > 8: <%= link_to_if(!comment.website.nil?, h comment.name, > comment.website) %> says: <%= h comment.body %> > 9: <hr /> > 10: <% end %> > 11: " > > In the error message, any idea why? Running rails 1.8.5. > > Thanks for your help! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Dec 2007, at 13:14, Richard Brashaw wrote:> > felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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.Yes - the empty string is not nil. You may find blank? appropriate: an empty string is blank, as is nil Fred> > > Thanks for your help! > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---