Rails 4/ERB if/else loop gives unexpected ;, expected : error, if I add html or text -
I am trying to create a mobile app in Rail 4 / ERB where people can post and comment on others' posts Are there. I want to be able to toggle the visibility of comments using jQuery, but whenever I try to wrap each loop in a div tag or add the number of comments, I get an error ('comment / The comment is partial with a template for the original comment)
This works, but shows all the comments (not what I want):
0? & Gt%; & Lt;% post.comments.order (: created_at) .reverse.each | Comment | & Gt%; & Lt;% = Render 'Comment / Comment', Comment: Comment% & gt; & Lt;% end% & gt; & Lt;%:% & gt; No comments yet. & Lt;% end% & gt; This gives me an unexpected semicolon error:
& lt;% if post.comments.count> 0? & Gt%; & Lt;% = post.comments.count% & gt; notes. & Lt;% post.comments.order (: created_at) .reverse.each | Comment | & Gt%; & Lt;% = Render 'Comment / Comment', Comment: Comment% & gt; & Lt;% end% & gt; & Lt;%:% & gt; No comments yet. & Lt;% end% & gt; does this:
<% if post.comments.count> 0? & Gt%; & Lt; Div & gt; & Lt;% post.comments.order (: created_at) .reverse.each | Comment | & Gt%; & Lt;% = Render 'Comment / Comment', Comment: Comment% & gt; & Lt;% end% & gt; & Lt; / Div & gt; & Lt;%:% & gt; No comments yet. & Lt;% end% & gt; The exact words of the error are syntax error, unexpected ;; Expected: What I am doing is wrong, and how can I use HTML without any errors in this block? Any help will be very much appreciated. Appendix: Oddly, if I stick the comment code inside each loop, it works (but, because it is each loop, it displays multiple times)
Appendix 2: It also returns an error:
& lt;% = render 'comments / form', post: post% & gt; & Lt; / P & gt; & Lt;% if post.comments.count> 0? & Gt%; & Lt ;! - & lt;% = post.comments.count% & gt; notes. - & gt; & Lt;% post.comments.order (: created_at) .reverse.each | Comment | & Gt%; & Lt;% = Render 'Comment / Comment', Comment: Comment% & gt; & Lt;% end% & gt; & Lt;%:% & gt; No comments yet. & Lt;% end% & gt;
Try this type
& lt ;% = Render 'Comment / Form', Post: Post% & gt; & Lt; / P & gt; & Lt;% if post.comments.count> 0% & gt; & Lt ;! - & lt;% = post.comments.count% & gt; notes. - & gt; & Lt;% post.comments.order (: created_at) .reverse.each | Comment | & Gt%; & Lt;% = Render 'Comment / Comment', Comment: Comment% & gt; & Lt;% end% & gt; & Lt;% Someone else & gt; & Lt ;! - No comments yet --- & gt; & Lt;% end% & gt; in line 2? Remove because post.comments.count> 0 will return true or false , so you have to check again. Therefore, you can now else condition.
Comments
Post a Comment