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
0
votes
2 answers

Set default date range to 1 week, and then have the entered date range persist through submit [Ruby on Rails]

I am creating a charts application that shows data based on a date range, and I want to have the date range default to a 1 week interval when the application is first opened, and then give the user the option to set the date range to search for a…
BeamerEA
  • 103
  • 1
  • 7
0
votes
1 answer

Rails Form not rendering json response

I am fairly new to rails, so please bear with me. I have a very simple form that takes a couple values and sends it to my EmailController. I run very basic logic on it and just want to return the JSON to the view. It looks like the controller…
Matt
  • 478
  • 4
  • 14
0
votes
1 answer

Best way to add css classes or html text in Rails views

Rails devs! I got a "best practice" question here. So I'm dealing with something very simple but that it's enough for me to understand. I got this header partial in my Rails views, and I'm using devise to authenticate users. I'm displaying in modals…
theKid
  • 522
  • 6
  • 19
0
votes
1 answer

Form fields autofilled on loading a signup page in Rails

I have this signup form code (the controller is added below) : <%= form_with(model: @user, class: "shadow p-3 mb-3 bg-info rounded", local: true) do |f|%>
<%= f.label :username, class: "col-2…
Irma
  • 31
  • 4
0
votes
2 answers

Chef - Using template erb with array

Just having issues trying to get this to work. Not exactly sure where I went but I'm guessing it might have something to do with the undeclared "group". So here is my code: Attribute File: default['dynamic']['config']['queueGroup'] = [ { …
kenochrome
  • 23
  • 3
0
votes
2 answers

Linking to profile page adding to URL based on current page

Working on a Ruby on Rails project using ERB. I am trying to link a user to their profile page. For example, clicking on the dropdown link and then profile should bring you to site.com/users/1 if you are logged in as User 1.
SJK
  • 135
  • 1
  • 10
0
votes
1 answer

Rails/Slim_Dynamic data tag attributes

https://github.com/slim-template/slim-rails/issues/168 How could I convert below erb to slim? ERB code
data-logined='<%=…
Crazy Pioneer
  • 33
  • 1
  • 10
0
votes
0 answers

undefined local variable Ruby Partial

I'm passing down some variables to a partial. model variable prints out just fine. But I cannot seem to add any other variables. I've tried it a few different ways. <%= render partial: 'layouts/modals/header', locals: {title: t('layouts.issue'),…
Raj Singh
  • 163
  • 1
  • 9
0
votes
1 answer

How to change code of every nth iteration

I got the following code in an .erb document: <% @blog.order("created_at DESC").each do |d| %>
0
votes
1 answer

Devise Sign in button unresponsive once placed in an html container

I am trying to get a better understanding of how to join my front end and back end code in my rails application. I have created a VERY basic devise user, simply to practice applying the html and css. The sign up and log in work perfectly fine, until…
Bertran
  • 1
  • 1
0
votes
1 answer

Prettier ERB conditionally adding element class

I'm trying to learn how to write more beautiful ERB. How could I format this to look prettier? <% site.data.navigation.each { |item| %> class="current"<% end %>> <%= item.name %> …
Justin Bishop
  • 157
  • 1
  • 8
0
votes
1 answer

ERB template binding not cascading to helper

I have the following code: user = "GG" erb = ERB.new "Hi <%= user %>!" puts erb.result(binding) resulting in the correct Hi GG! If I create a helper: module Helper def hello "hello" end end include Helper erb = ERB.new "Hi <%= hello…
Don Giulio
  • 2,946
  • 3
  • 43
  • 82
0
votes
3 answers

link method: :delete HTML

I am relatively new to programing and just wondering if there is a way to have something like = link_to "sign out", destroy_user_session_path, method: :delete, but using an link. EDIT Let me clarify, I did use = link_to "sign out",…
0
votes
0 answers

Adding a dynamic field via javascript on RoR

I would like to add dynamically fields to a form in a ERB file and I'm getting an issue that I don't know how to fix. Basically, a user clicks a btn on a form and a new input text field is added via javascript. Right now, my code looks like this: …
Pataquer
  • 283
  • 1
  • 14