Avoid rendering comments in HTML - html

I have a lot of comments in Rails views.
How i can prevent rendering them ?

If I understand the question correctly, you're asking about Ruby/Rails comments vs HTML comments... Give this a try in your view:
<!-- This is an HTML comment and will show up in the HTML source! -->
Now try this:
<%# This is a comment that won't show up in the HTML source! %>
<%#
You can even use this for commenting multiple lines!
How useful!
%>
Does that help?

use =begin and =end to mark the beginning and end of your comment

There is no easy way to do that. You can monkey patch ERB sources, perhaps, but it is a bit nerdy.

I'm not a Rails programmer, but a quick but of Binging brought up this link:
http://blog.brijeshshah.com/strip-tags-in-rails-javascript-and-php/
The approach he's using is one that I've used in the past where you sanitize the view's output. sanitize being the name of the function you want to use before rendering the view.

Maybe you can use Haml Comments: -# allow to comment your haml code without them appearing in the generated html.

Kind of hackish, but you can wrap it in a helper method
In your view:
<% comment do %>
<%= "this won't be executed" %>
or displayed
<% end %>
in app/helpers/application_helper.rb
module ApplicationHelper
def comment(&block)
## you can do something with the block if you want
end
end

Related

Escaping '<' character in Foundation for Email, Zurb

I'm using https://foundation.zurb.com/emails.html to develop my email templates. For each and every template there are parts where I need to add eg. <%= params.someData %> but '<' gets escaped while compiling, so instead of being <%= params.someData %> it generates to <%= params.firstName %>
I see that https://foundation.zurb.com/sites/docs/panini.html is used for compiling but I'm not able to disabled this functionality in order for < character not to be escaped.
Please help.
You can use the integrated raw helper to do this.
Either use {{{{raw}}}}...{{{{/raw}}}} or <raw>...</raw>.
In gulfile.babel.js use pipe to replace <%= with <%=. I have had to do this with Adobe Campaign code, which doesn't work well with Zurb Foundation for email.
.pipe($.replace, '<%=', `<%=`)
Good luck.

Rails : bad habit of using .html.erb as helpers?

I am using a lot of .html.erb partials as "helpers". I'm not sure if this is a really bad habit or not.
main_file.html.erb
render 'shared/some_helper.html.erb',
param_1: param_1,
param_2: param_2,
...,
optional_param_1: optional_param_1,
...
shared/some_helper.html.erb
<%
optional_param_1 ||= optional_param_1_default
optional_param_2 ||= optional_param_2_default
...
# Some additional logic
%>
<!-- The HTML that uses the params -->
I feel this is justified because my helpers are more about HTML than ruby/rails.
But is there actually a better way to do it (that remains as convenient) ? Maybe define some function in applicaton_helper.erb that would output the HTML ? But then I won't be able to write HTML...

How to escape all HTML in a block in Rails ERB view

Essentially I want something like the following:
<code class="snippet">
<%= html_escape do %>
My markup displayed to user
<% end %>
</code>
However the html_escape method does not accept a block. If this is not built into Rails API somewhere else, perhaps using some helper, does anyone have advice on how to make a custom helper where the yield statement output is captured into a string that I can then escape myself?
Thanks,
Keith
Rails' capture and escape_once helper methods can create a String from a block in an erb template and then output an escaped version of it:
<% snippet = capture do %>
My markup displayed to user
<% end %>
<code><%= escape_once snippet %></code>
content_for is another helper that provides similar functionality to capture, that you may consider using depending on the situation.
To explain, snippet is an ActiveSupport::SafeBuffer, and is why escape_once is needed. You could achieve the same by calling snippet.to_str instead of escape_once snippet (However .to_s will not work as that is different to .to_str in ActiveSupport::SafeBuffer).

html.erb doesn't recognize html tags?

New to RoR, so please don't kill me ;)
Was wondering why does not Rails 3 recognize HTML tags retrieved from database?
For example,
Name Content
Title <b>Great</b> Show Edit Destroy
I wanted to have Content to be bold and put < b > tag around it, when it retrieves from a database it looks like a plain English.
Any thoughts?
Thank you in advance.
If I got this right, you need to do this:
<%= myrecord.content.html_safe %>
to get "real" html and not just escaped html code.
Even though it is unsafe to deliberately output HTML from the DB, you should call raw on the content you're trying to print.
<%= raw #object.my_content %>

Rails content_tag helper for simple things?

Should I be using the content_tag helper for all html tags when working with Rails?
Is it The Rails Way to use content_tag for even simple things like Header tags?
<%= content_tag :h2, :class => "bla bla" do %>
Header
<% end %>
vs.
<h2>Header</h2>
Clearly just using straight html is much 'simpler' and 'shorter', but what is the correct Rails Way of doing things?
Using content_tag when you don't have to is a waste. There's no need to use ERBisms to generate static HTML so don't do it. If some other piece of code determines what tag to use, then you'd use content_tag to construct that tag.
If you are asking the rails way of doing this, then its defiantly using 'content_tag', but using tag_helpers has its own advantages and disadvantages
Personally for me I can see these things, (Using rails helpers instead of pure HTML)
Advantages
1 - Your code will be cleaner. (with less lines)
2 - You will have more control other the elements.
Ex: You can have your own helper tags like 'big_text_box' which will return a text box more than the normal with and you can use it across all the site
3 - You will be able to add attributes like class, id dynamically in the runtime
Disadvantages
1 - If you have a separate designer (I mean UI engineer) he/she will get confuse of by the code you have use. As its not pure html
2 - Its slow than the pure html (But this will not even noticeable unless otherwise your app is a damn major one...)
So its up to you to decide what to use, personally I prefer using rails helper tags as it makes me more comfortable
HTH
cheers
sameera
One useful method is the "div_for", which is somewhat similar to the content_tag. If you find yourself marking up HTML elements with data you can reference later, "div_for" makes your life much easier.
Let's say you have a bunch of people being shown on a page and you need to wrap each with a div that has a unique ID so you can modify these elements with JS. By hand and straight HTML you would have to do:
<% #people.each do |p| %>
<div id="person_<%= p.id %>"><%= p.name %></div>
<% end %>
That would get bothersome if you were doing LOTS of this with multiple attributes (I commonly use custom IDs, classes, and some data attributes). But using div_for you could write the above as:
<% #people.each do |p| %>
<%= div_for(p) do %><%= #person.name %><% end %>
<% end %>
Makes the HTML a little easier to read when things get long and complex. I found it is much cleaner when working with javascript a lot.
http://apidock.com/rails/ActionView/Helpers/RecordTagHelper/div_for