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

Need to specify max length for numeric filed type in simple_form

I need to restrict the user from entering the only integer for one of my column with length specification. <%= f.input :value, as: :numeric, :input_html => { :maxlength => 4 } %> The above works for string type of field in the UI, but when I add…
Ferdy
  • 666
  • 7
  • 25
0
votes
2 answers

Rails: How to create a Add more button which add some textfield in erb form with jquery

I am creating a form in which quote_amounts is contains Hash. I want to create a form with a button in which when i click on button new set of textfields opens for ex: when a form opens there should be one 2 textfields: label & amount, and when i…
Dhruvil Dave
  • 109
  • 1
  • 20
0
votes
1 answer

Transforming text to bold in ruby

I have both controller and view for a simple game I've created. This is my function : def score @letters = params[:letters] @word = params[:word].upcase if !compsrison?(@word.split(''), @letters.split) @result = "Sorry, but…
Aga
  • 1
  • 1
  • 3
0
votes
1 answer

Ajax response in erb from ruby method defined in controller

I have a ruby controller file with various methods defined, i want to get response of one of the method in erb i.e frontend using ajax call. I am a beginner in ruby, so doesn't have much experience of handling requests in ruby, when i try to run the…
ankita28
  • 3
  • 4
0
votes
1 answer

Ruby System Call Executing Before Script Finishes

I have a Ruby script that produces a Latex document using an erb template. After the .tex file has been generated, I'd like to make a system call to compile the document with pdflatex. Here are the bones of the script: class Book # initialize the…
michaelmichael
  • 13,755
  • 7
  • 54
  • 60
0
votes
1 answer

Ruby template indentation issue - puppet

I'm unable to fix indentation for ruby template, I've spent hours on this and still couldn't get it to work as expected. Here is the template: <%= @log_path -%> { <%= @rotate_every %> rotate <%= @rotate_count %> compress <% if @delaycompress…
user630702
  • 2,529
  • 5
  • 35
  • 98
0
votes
1 answer

Rails app not loading stylesheet or js file

In my content/themes/default/views/layouts/default.html.erb file I have these set: <%= stylesheet_link_tag 'default' %> <%= javascript_include_tag 'default' %> those files are in content/themes/default/assets/stylesheets and javascripts…
Tony
  • 8,681
  • 7
  • 36
  • 55
0
votes
1 answer

Why is Spree not rendering my changed template?

Working on a Spree Commerce app with Rails and trying to customise the templates on the frontend. I have cloned the frontend templates library and they are located in my app/views/spree directory, but when I make a change there it is not rendered in…
kykyi
  • 375
  • 4
  • 10
0
votes
3 answers

WickedPDF template image_tag / wicked_pdf_image_tag not working on Rails API-only

I'm trying to generate a PDF with Wicked PDF. So far it's going fine, but when I try to add images all I get are blank boxes. My images are in both ../app/assets/images/foo.png and ../public/images/foo.png for testing purposes. From what I…
0
votes
1 answer

Puppet RUBY template - Custom ruby fact is adding line even if the file doesn't exists

I've created two ruby facts ecdsa.rb and ed25519.rb and it checks if file exists. If it does exists then add a line to a file. But it adds both lines even though the second file doesn't exists. Facter shows that the second file doesn't…
user630702
  • 2,529
  • 5
  • 35
  • 98
0
votes
1 answer

Erb template not rendering when used with ensure exception handling

I have come across a issue when writing some sinatra code I have the follow block of code begin # do stuff here rescue SomeException::Class => ex flash.now[:err] = "some error " + ex.message ensure erb :content, :layout => :mainlayout…
user427165
0
votes
1 answer

What are the reasons to use erb integration for webpacker in Rails 6?

I have surfed a couple of hours through the web but couldn't find any articles/walkthroughs/comparisons touching erb integration of webpacker. I've found 1 question, unfortunately, the author haven't read docs attentively and the answer was right…
Alexander Gorg
  • 1,049
  • 16
  • 32
0
votes
1 answer

Ruby Undefined Local Variable

The following is code from an ERB tutorial. When I tried to execute the code, the compiler complained saying "(erb):16: undefined local variable or method `priority' for main:Object (NameError)". I cannot figure out the reason. Could someone please…
Terry Li
  • 16,870
  • 30
  • 89
  • 134
0
votes
1 answer

modal form submit without page refresh and redirect in rails 5

Does anyone know how to submit this modal form without redirect to update_mobile_number action and without reload view screen? I got this error while I submitting the form error POST http://localhost:3000/web/sessions/update_mobile_number 406 (Not…
Suman Das
  • 301
  • 1
  • 4
  • 18
0
votes
0 answers

set input value from url text

I have a string passed in url so I want to retrieve a text from this url for example https://www.mywebsite.com/path/text_to_retrive and put in input This is my code : <% %> <% valid_access_code = @room.access_code.nil? || @room.access_code.empty?…
nightmare
  • 1
  • 1