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
1 answer

Alternatives to using 'render' to parse a (executed) view file with Nokogiri

I have a helper file which includes a method which contains the following snippet: header_id = Nokogiri::HTML.parse((render "shopfront/headers_#{@current_user.shop.header_id}")) What I'm doing is getting the result of the partial contents (which…
Chris
  • 438
  • 4
  • 17
0
votes
1 answer

CSS not showing up with ERB

I've just hosted something on Heroku, and whenever I opened the app it worked just fine with the css being linked as such: The problem came when I added my custom domain, the css no longer…
captDaylight
  • 2,224
  • 4
  • 31
  • 42
0
votes
1 answer

How to use AJAX to reload a section of page unrelated to rails controller action

My web app follows the structure as described in previous questions here and here. See the picture below of the interface. What I want is, when the user edits/updates verification through the switch icon (submit button automatically), also described…
axelmukwena
  • 779
  • 7
  • 24
0
votes
1 answer

How to create nested model from partial Rails 6

Newbie Here! I'm unsuccessfully trying to submit a form of a nested model. Aim: Verify a translation by submitting a boolean to a reviews model to associate whether a translation is verified or not, with translation entry and user model associated…
axelmukwena
  • 779
  • 7
  • 24
0
votes
1 answer

In a Rails view, how do I display dynamic code within a HTML code snippet?

Within a Ruby on Rails view, how would I display a HTML code snippet that includes dynamically-generated code? For example, users see different HTML code snippets depending on their user.url (where url is stored against a User record in the…
nope2023
  • 1,668
  • 1
  • 15
  • 28
0
votes
2 answers

Rails HTML Manipulation with JQuery

I am having issues getting JQuery to be parsed by ruby. For instance, the following line of code is supposed to add the @user's username after the div with id 'title.' $('#title').after('<%= escape_javascript(@user.first.username') %>); However,…
TimeEmit
  • 4,516
  • 4
  • 18
  • 19
0
votes
1 answer

Collection_select helper cannot build disabled html tag in ruby on rails erb

Ruby on rails docs says you can add html options to tag helpers. The code below is not working. My select box cannot shown disabled. Is there anybody knows why? <%= form.fields_for :journal_doc_analytics do |analytics| %>
0
votes
2 answers

How can I have alternating HTML table row classes when using an embedded Ruby iterator?

Below I have part of an index.html.erb file with an HTML table and my goal is to have alternating white and gray rows while using a Ruby iterator. I want odd rows to have & even rows to have . I am using…
user13695646
0
votes
1 answer

How to generate Rails form dynamically dependant on languages

I have a form and I want to show some form elements x times dependent on languages in Langs table, it is nested attributes with has_many association. I am trying to use fields_for but with no success. I have a Lang objects and this code in…
Levi
  • 77
  • 7
0
votes
2 answers

Rails: undefined method text_field_tag

My ERB file works fine if I use text_field, but if I switch to text_field_tag I receive this error: undefined method `text_field_tag' for # Here is the code that works: <%= f.text_field mystring…
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
0
votes
1 answer

Rails: Form with configurable number of inputs

I wrote a form that generates a text input for each property. The list of properties is configurable by the customer. <% properties = ["refractivity_at_2kHz", "refractivity_at_5kHz"] %> <% properties.each do |property| %>
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
0
votes
1 answer

Why does this sign in form fail with no errors?

TL;DR Why does my user sign in form not create a new session/why does it fail without errors? I'm pretty new to rails, and I'm having some confusion about user sign in forms that create new sessions. Essentially I can either sign the user in without…
schnondle
  • 121
  • 14
0
votes
1 answer

Stack trace showing ERB syntax error. Is the problem with app code or ERB library?

I'm working through an ERB chapter from a Lynda.com course, Ruby Essential Training Part 3: Files, Formats, Templates, and keep getting a syntax error in what appears to be ERB itself. When I enter this code: #!/usr/bin/env ruby #### Mail Merge…
user2079696
0
votes
1 answer

Ruby view unable to map values to boolean

I want a radio button with 2 value. It's default value is set as false. Currently, the data is not saved into the database. I have scoured SO and look at ruby's form_helper docs and still can't seem to find out what's wrong with my code. This code…
Peter
  • 437
  • 1
  • 4
  • 20
0
votes
1 answer

Why is Rails skipping my view loop after failed save?

I have a form that creates a new nurse and a list of links below it which are created from the existing nurses. The list renders fine on first load, but after a failed save of the form, I get an error indicating I'm missing the nurse_id part of the…
Eojo
  • 35
  • 8