I cannot find it anywhere but in the project I am working with.
It doesn't work in the same way as <%= (I tried to change), but I can't understand the difference.
<span class="option-content" placeholder="<%=t('pages.edit.option')%>">
<%%= content %>
</span>
The ERB dock here says
<% Ruby code -- inline with output %>
<%= Ruby expression -- replace with result %>
<%# comment -- ignored -- useful in testing %>
% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
%% replaced with % if first thing on a line and % processing is used
<%% or %%> -- replace with <% or %> respectively
this means the
<%%= content %>
will will be replaced with
<%= value of content %>
In short, ERb processes double-percent marks into single-percent marks.
It looks like you're using one layer of ERb templates to generate another layer of ERb templates.
The first layer of ERb doesn't need a variable called content, just the t method:
<span class="option-content" placeholder="<%=t('pages.edit.option')%>">
<%%= content %>
</span>
That first layer is rendered to produce the second layer:
<span class="option-content" placeholder="Edit">
<%= content %>
</span>
As you can see, that is also an ERb template. I expect that something else, later on, takes that second ERb and uses it to render something like:
<span class="option-content" placeholder="Edit">
Hello, world.
</span>
Related
(As far as I have researched here, this is not a duplicate question. Trimming spaces -- often trailing newlines -- is being discussed for <%- or -%>, but not for <%=. It could be a minor defect in Erubi template engine as well, the one being used by Rails for ERB templates.)
I want to render / syntax-highlight code in a view, and my ERB view template contains:
<p>
<strong>Code:</strong>
<pre class="highlight github">
<%= highlight(#code.code, #code.language) %>
</pre>
</p>
The result is that the HTML output is:
<p>
<strong>Code:</strong>
<pre class="highlight github">
<span class="kt">[and here's the code, but indented too much]</span>
</pre>
</p>
Because of the pre tag, the spaces in front of the first code line are included in the HTML and thus rendered, resulting that the first code line is indented with four spaces too much.
Obviously, I can also make the ERB view template as:
<p>
<strong>Code:</strong>
<pre class="highlight github">
<%= highlight(#code.code, #code.language) %>
</pre>
</p>
but that looks ugly (because the indenting is off) in my template view.
Question: how can I make the <%= also swallow leading spaces? I know that using -%> as closing tag removes trailing spaces/newlines... but I want the leading spaces (not just newlines) to be removed as well.
Try using the concat helper method with an ERB tag that starts with <% instead of <%=:
<p>
<strong>Code:</strong>
<pre class="highlight github">
<% concat(highlight(#code.code, #code.language)) %>
</pre>
</p>
I have a segment of code:
<% #public_address.each do |key| %>
<%= key["address"]["address"] %>
<% end %>
This displays the keys properly, but this code
<% #public_address.each do |key| %>
<% puts key["address"]["address"] %>
<% end %>
displays nothing. What gives? What's the difference between the two?
The <% %> and <%= %> are used in erb to execute ruby code when rendering a template.
Erb is the default template engine in rails.
Difference between <% %> and <%= %>
<% %> Will evaluate the ruby code it contains, but "silently".
Meaning that no output is going to be printed on the rendered page.
<%= %> on the other end, evaluates the ruby it contains and
renders the result on the rendered page.
What's the difference between <% code %> and <%= code %> in Rails erb?
What's puts?
Puts is simply a method from Ruby that is used to print a string at runtime. It has nothing to do with erb templates.
In your first bit of code <%= key["address"]["address"] %>, the <%= %> is rails syntax for evaluating the code inside and returning the value.
In your second bit of code <% puts key["address"]["address"] %>, you use <% %>, which doesn't return an evaluated rails statement. Furthermore, puts is a method that outputs whatever follows it to the stout object. In a command line program, that means printing out to the terminal screen, but in a web app you aren't working with a terminal screen. You are working with controllers and view templates, so it is necessary to use the evaluative <%= %> if you want to return values that will be displayed in the view.
I basically want to do this:
<% #videos.each do |vid| %>
<div id=vid.location>
...
<% end %>
how do I evaluate vid.locaion and use it as the id attribute?
i've tried the above, id="#{vid.location}" and id="<% vid.location %>" (the last one with and without quotes.
any help appreciated
Easy,
<div id="<%= vid.location %>">
Your first attempt was wrong as you're still in markup - not ruby. In the last one you used <% rather than <%=, so while it was evaluating the getter, it just didn't present it to your view.
Here is my code, I'm trying to display a list of links to a bboy's crews in sentence form with .to_sentence
<span class="affiliation">
<% if(#bboy.crews.count > 0)%>
<span><%= if(#bboy.crews.size > 1) then "Crew".pluralize else "Crew" end %>:</span>
<%= #bboy.crews.collect{|c| link_to c.name, c}.to_sentence %>
<% else %>
<em>Independent</em>
<% end %>
</span>
The output I get is the correct links but it displays as:
Hustle Kidz and Knuckleheads Cali
rather than:
Hustle Kidz and
Knuckleheads
Cali
with the html escaped, rather than the desired links.
Am I missing something? I've tried CGI.unescapeHTML and a few others but am getting lost...
Rails 3 now automatically escapes everything, in order to output raw HTML use this:
<%= some_string.html_safe %>
or this:
<%= raw #some_html_string %>
Thanks to macek for a hint.
For additional details: http://markconnell.co.uk/posts/2010/02/rails-3-html-escaping
You can (and should) you the raw method
<%= raw #some_html_string %>
I agree with Kleber S, you should move this into a helper, because that's a lot of logic for a view
def crews_description(crews)
if crews.empty?
content_tag('em', 'Independent')
else
label = "Crew"
label = label.pluralize if crews.size > 1
crews_links = crews.map {|crew| link_to(h(crew.name), crew)}.to_sentence
content_tag('span', label) + crews_links.html_safe
end
end
and in your view:
<span class="affiliation">
<%= crews_description(#bboy.crews)
</span>
I recommend you move this block of code to an helper and then use the .html_safe method to obtain the expected results.
I'm new to Ruby and Rails and I have a simple controller that shows an item from the database in a default view. When it is displaying in HTML it is outputting <p> tags along with the text content. Is there a way to prevent this from happening? I suppose if there isn't, is there at least a way to set the default css class for the same output in a statement such as this:
<% #Items.each do |i| %>
<%= i.itemname %>
<div class="menu_body">
Link-1
</div>
<% end %>
So the problem is with the <%= i.itemname %> part. Is there a way to stop it from wrapping it in its own <p> tags? Or set the css class for the output?
Thanks!
You need to enclose it with the HTML tag of your choice. Also if required you can escape bad code by using <%=h i.itemname %> Example:
<% #Items.each do |i| %>
<div><%=h i.itemname %></div>
<div class="menu_body">
Link-1
</div>
<% end %>
Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.
You canchange the public/stylesheets/scaffold.css if you want.
Or if you want to change it for a single page say items/index.html.erb
<style>
p{
/* your style here *?
}
</style>