Questions tagged [erb]

ERB is a simple templating system for Ruby, embedding code in any plain-text document. It is often used for HTML generation in web frameworks (such as Ruby on Rails).

ERB stands for Embedded Ruby and is a markup-based way of embedding Ruby code into text files.

The tag may be used for the standard (pure-ruby) ERB library, the C-based implementation eRuby or the even-faster Erubis library. ERB ships with Ruby installations as part of the Standard Library; the documentation for ERB is available on ruby-doc.org.

ERB is commonly used as templating system, for example in producing HTML pages with dynamic content (such as in the Ruby on Rails web framework) but is also useful in a variety of other contexts, such as code generation, mass emailing, or simple text reporting.

ERB markup is generally seen as <% ... %> and <%= ... %>. See the section "Recognized Tags" in the documentation for the ERB class for the full set of markup.

A simple example:

template.erb

Hello, <%=name%>. Are you:
<% moods.each_with_index do |mood,i|%>
  <%=i%>. <%=mood%>
<% end %>

code.rb

require 'erb'
template = ERB.new(IO.read('template.erb'))
name = "Phrogz"
moods = %w[ Happy Angry Sad ]

message = template.result(binding)
puts message

#=> Hello, Phrogz. Are you:
#=> 
#=>   0. Happy
#=>   1. Angry
#=>   2. Sad
2617 questions
29
votes
8 answers

How to pass a javascript variable into a erb code in a js view?

I have this Javascript view in my Rails 3 project: app/views/expenses/new_daily.js.erb var i = parseInt($('#daily').attr('data-num')) + 1; //$('#daily').append('agrego fila ' + i + '
'); $('#daily').append('<%=…
fespinozacast
  • 2,484
  • 4
  • 23
  • 38
29
votes
6 answers

ERB Template removing the trailing line

I have an ERB template for sending an email. Name: <%= @user.name %> <% if @user.phone.present? %> Phone: <%= @user.phone %> <% end %> Address: <%= @user.address %> I am trying to remove the blank line between Name and Address when Phone is…
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
28
votes
5 answers

erb in coffee script with rails 3.1

I would like to use some erb in my .coffee files, like the following example myLatlng: new google.maps.LatLng(<%=@location.latitude %>, <%=@location.longitude %>) I renamed my locations.js.coffee to locations.erb.coffee but I still get the…
denisjacquemin
  • 7,414
  • 10
  • 55
  • 72
28
votes
3 answers

Dynamic class name in HAML

Is there a better way to convert the following to HAML? <% flash.each do |key, value| %>
<%= value %>
<% end %> Best I can do is: - flash.each do |key, value| %div{:class => "flash " + key.to_s}=…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
28
votes
4 answers

defined? method in Ruby and Rails

I have a quite old templating system written on top of ERB. It relies on ERB templates stored in database. Those are read and rendered. When I want to pass data from one template to another I use the :locals parameter to Rails render method. For…
Honza
  • 4,349
  • 2
  • 24
  • 40
28
votes
4 answers

"undefined method `errors' for nil:NilClass" when calling on errors method

I am currently teaching myself some RoR and doing the tutorial, but adding some nicer layout and stuff with bootstrap and I am running into a problem which I cant figure out. I am trying to do the validation part…
tomr
  • 1,134
  • 2
  • 10
  • 25
26
votes
5 answers

Rails 3 render action from another controller

I need to render another controller action <%= render "controller/index" %> and i get this error Missing partial controller/index with {:formats=>[:html], :locale=>[:en, :en], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder]} in view paths…
Mihai
  • 1,254
  • 2
  • 15
  • 27
26
votes
3 answers

How to include a css or javascript in an erb that is outside the layout?

Sorry for the slightly noobish question, as I am writing my first rails app. I get the idea of the layout view, but if you are using them, is there any way to include a view specific js or css file? For example, I have layouts/products.html.erb, and…
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
26
votes
6 answers

rails page titles

I don't like the way rails does page titles by default (just uses the controller name), so I'm working on a new way of doing it like so: application controller: def page_title "Default Title Here" end posts controller: def page_title …
tybro0103
  • 48,327
  • 33
  • 144
  • 170
25
votes
5 answers

How to set html on data-disable-with to rails submit_tag

I have a RoR app using bootstrap. I'm trying to apply the fontawesome html icon tag to a submit_tag helper, but it does not seem to be supported. When I click submit, the disable content just appears as a string instead of being interpreted to…
Slenny
  • 379
  • 2
  • 4
  • 13
25
votes
3 answers

simple_form with Bootstrap check box

I'm using simple_form with Bootstrap and I would like to have my "Remember me" check box be inline to the left of the label, as in the Twitter Bootstrap docs: http://twitter.github.com/bootstrap/base-css.html#forms My form code looks like this: <%=…
hamsterdam
  • 599
  • 1
  • 8
  • 15
23
votes
2 answers

using HTML5 video tag in a simple rails app

inside index.html.erb there is the following code I am not sure where to put my mp4 video file so I…
delta2006
  • 425
  • 2
  • 4
  • 13
23
votes
3 answers

Conditional tag wrapping in Rails / ERB

What would be the most readable and/or concise way to write this in ERB? Writing my own method isn't preferable, as I would like to spread a cleaner solution for this to others in my company. <% @items.each do |item| %> <% if item.isolated? %> …
Joseph Ravenwolfe
  • 6,480
  • 6
  • 31
  • 31
23
votes
3 answers

Relative Paths From Partials Referencing Other Partials

I am using a (primary) partial: <%= render partial: 'shared/page/head' %> Which makes use of other (secondary) partials: <%= render partial: 'shared/page/head/title' %> <%= render partial: 'shared/page/head/meta' %> ... <%= render…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
22
votes
3 answers

Check if a variable is undef in puppet template

What is the proper way to check if a variable is undef in a puppet template? In the manifest the variable is defined as follows $myvar = undef How is this checked in the template? Is saw the following two variants <% if @myvar -%> <% end…
André Keller
  • 3,179
  • 3
  • 15
  • 23