How to comment code in Rails views? - ruby-on-rails

As I'm playing with Rails and developing views I often want to comment out code. Simple enough with classes & models but views are a bit more tricky.
What's best way to comment code in a view so it's not interpreted by, well, anything... HTML gives us <!-- commented Rails code here --> though code enclosed here seems to get interpreted anyway?!? Or is there a more Railsy way?

<% code code # comment %> USED to work but I think that was accidental.
You were always supposed to put comments in separate comment tags <%# comment %>
Note NO SPACE before the pound.
Now the old loophole is closed (I forget whether 'now' means Ruby 1.8 or Rails 3 or what) so that:
<% code code # this runs too %>
<% # also runs %>
<%# the only way to comment out %>

I use this all the time
<%# This is a comment %>

The reason Ruby code would be executed inside <!-- --> HTML comments is because all of the server side code (ie. Ruby) is interpreted first, and then the output is sent to the client, at which point the browser interprets <!-- --> as a comment. As the other answers said, use <% #comment %> to comment within a Rails view.

Although (and i'm hoping to be corrected here) you have to be careful because i've had some really strange behavior when doing something like this:
<% if (my_boolean) # Commenting on this if-block %>
where it will affect the HTML that directly follows that (even if it's on another line).
Anyone?
And would this qualify as an answer, or a comment?

<% #comment here %>
:D

Related

How do I get sidebar to be rendered on left when viewing object?

I'm pretty new to rails so sorry if this I'm not clear. I have an object called projects in layouts I have _left_column.html.erb that adds stuff to the left column based on what page I'm on. In this case the stuff is info of other objects that are associated with it (things like tasks).
Screenshot:
I works as shown but only for the project with id of 1. I'm not sure how to get this to work on all projects. Thanks!
I'll try to give some tips for your code
<% case request.path.split('?').first.to_s %>
<% when 'projects/1' %>
# this is the problem,
# all code below when 'projects/1' only running if projects equal to 1
# if you want all code below running you can try remove the logic when
<% end %>
<% end %>

Multiple form_tag in rails application

This is the code in my logins_form.html.erb
<%= form_for(#login) do |f| %>
// code here
<%end%>
<%= form_tag(:controller=>'posts', :action=>'index') %>
// code here
<%end> --1
<%= form_tag(:controller=>'logins', :action=>'create') %>
// code here
<%end%> --2
It is only accepting one of the 1 or 2 not both. Why so? Even if I remove one of the two, both the forms are redirecting to logins.
What am I doing wrong?
Thanks.
Did you copy/paste your exact code?
If so look at your first form's end and you will notice your missing the % in the closing %>, which will cause the code to improperly compile the erb template.
You should be able to use two forms fine, as long as you are not trying to nest them within each other.
Would be interesting to see HTML output because you can't have nested forms on your page check question

This particular line of code seems to work with Rails 2.x.x but not 3.x.x? Can someone please help to "translate" it?

<li<% if #flits.first == flit %> class="first" <% end %>>
I created css for #flits_list and #flits_list :hover in application.css in Rails 3 but I would like the first flit in the list (flits_list.first) to have different css so I created a class, but this code returns the error
no method error in home#index. you have a nil object when you didn't expect it! You might have expected an instance of array. the error occurred while evaluating nil.first
Any help would be greatly appreciated.
The problem is that #flits is nil, presumably because your all_flits method is returning nil.
However, I'd recommend not putting that logic in the view, breaking up a tag like that. You have several options to make it cleaner:
Option 1: Use the CSS pseudo-class first-child like so:
li:first-child {
...
}
This has the advantage of not requiring any back-end logic or special markup. The only downside is that it has spotty older browser support, e.g. IE6.
Option 2: Use the Rails tag helpers.
<%= content_tag :li, :class => #flits.first==flit?"first":"" %>
Option 3: Tuck it away in a helper method
<%= li_for_flit %>
Then in the helper:
def li_for_flit
#spit out your tag here
end

Rails Syntax highlighter albino is not working

I am using syntax highlighter "albino" i my rails project ,but it is not displaying any thing
below i have written the code
in helper model
def highlight(text)
Albino.new(text, :ruby)
end
In the view
<% #codes.each do |code| %>
<%= highlight(code) %>
<% end %>
so can any one help me where i am going wrong
or suggest any good highlighter gem for rails?
Which errors do you get?
You are missing a . after #codes:
<% #codes.each do |code| %>
<%= highlight(code) %>
<% end %>
It looks to me like your helper is creating a new Albino instance but not using actually asking it to syntax highlight.
Change your helper as follows:
def highlight(text)
Albino.colorize(text, :ruby)
end
Have you considered using Google Code Prettify? It's the syntax highlighter used on both Google Code and Stack Overflow, which is likely to mean it's reasonably robust.
I don't normally like putting too much functionality in JavaScript, but it seems to me that syntax highlighting is a reasonable feature to add in this way - after all the code will still be readable without the highlighting.
This is a bit of an old problem, but I just came across it myself.
The problem is that Albino is outputting HTML directly as it's being parsed (I think that's the right word, I'm quite new to this).
For example:
highlight(text)
And text is:
def hello_world
puts "Hello World!".to_s
end
Will result in:
<div class="highlight"><pre><span class="k">def</span> <span class="nf">hello_world</span> <span class="nb">puts</span> <span class="s2">"Hello World!"</span><span class="o">.</span><span class="n">to_s</span> <span class="k">end</span> </pre> </div>
What needs to be done is add .html_safe into your highlight method.
Albino.colorize(text).html_safe
That should work.

Rendering a variable with erb

I've got the following problem: I have rhtml (html minced together with ruby inside <% %> and <%= %> tags) stored in a database which I want to render. The information is acquired through a query. I need to be able to evaluate the information I get from the database as though as it was normal content inside the .erb-file. What I currently have:
<% #mymods.each do |mod| %>
<%= render_text(mod["html"])%>
<% end %>
Where mod["html"] is the variable containing the rhtml-code and #mymods an array of objects from the query. I have currently no idea what function I should use (render_text does, of course, not work).
Help is greatly appreciated.
/TZer0
You can use the ERB object to render text without the text being in a file.
Just pass the text with the <%= %> tags. You could put something like the following as an application_helper function.
def render_erb_text(text, args={})
b = binding
template = ERB.new(text, 0, "%<>")
template.result(b)
end
And then in your template
<%= render_erb_text("<%= %w(hi how are you).join(' - ') %>")%>
You might also consider rendering the text in your controller as you can handle any render errors better there than during view evaluation.
Take a look at the ERB documentation for more information regarding variable binding etc.
I'm not familiar with the details of how this works under the covers, but there could be some serious risk in running this code on bad or malicious database data. Evaluating ruby code from user input or any un-vetted source should be done very carefully, if at all.

Resources