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

How to define image tag in index.html.erb?

I am following a simple process to call images from database. I will explain my process and problem so that it is easy for you to answer. In assets/images I will save my images. In the database I have created t.images:string and will give the…
0
votes
1 answer

How to read/translate *txt.erb template (ruby) with python script

I have a quite complex software system which was developed in ruby and has been now all "translated" and transported into python. The last thing which is left is a series of *.txt.erb templates. I would like to leave them as they are but have a…
Eli
  • 1
0
votes
1 answer

$(document).ready() event not running

I have jQuery loading correctly on Rails 3, and the $(document).ready() event never gets run. I know this because I tried running the code within the $(document).ready(); manually via Google Chrome's Javascript Console and it ran just fine. I'm not…
Jarred Sumner
  • 1,793
  • 6
  • 33
  • 45
0
votes
1 answer

rails6 / receiving js.erb response from vanilla js request

I'm sending simple ajax call : axios({ method: "post", url, headers: { "Content-Type": "application/javascript", "X-CSRF-Token": headerToken() } }) to the following action : def search @products =…
Ben
  • 5,030
  • 6
  • 53
  • 94
0
votes
2 answers

Error creating a new form: ActionView::Template::Error (undefined method `title'

Can someone help me make a form? I keep getting ActionView::Template::Error (undefined method `title' for #): UrlsController ''' class UrlsController < ApplicationController def url_params params.require(:url).permit(:title, :link) end …
0
votes
1 answer

Remove array from showing under my rails erb-form

I have a checkbox form where you can select many customers to attend a single event. The form works but there is an array of all customers under it and I can't figure out how to remove it. events.rb def addcustomer @event =…
Robert
  • 79
  • 1
  • 2
  • 8
0
votes
2 answers

How can I send subject parameter to controller using remote_function?

Suppose this form: <% form_for(@student) do |f| %> <%= f.select(:subject_id, options_from_collection_for_select(@subjects, :id, :name), {:prompt => 'Select a subject' }, {:onChange =>…
Israel
  • 3,252
  • 4
  • 36
  • 54
0
votes
1 answer

How to list only object key from s3 in Ruby on Rails?

I have a rails app that's connected to my AWS s3 bucket. I'm trying to simply list all object keys only. But for some reason my code is not JUST returning the object key (the file name) but it's returning all data bout the object. How do I get…
Wes Creations
  • 337
  • 2
  • 10
0
votes
1 answer

How to access a java script variable in render partial url of Rails ERB file?

I have below piece of code in results.js.erb of Rails application where I need to render the partial URL dynamically using the string interpolation concept. I was unable to interpolate selector value dynamically in the render. Any help in resolving…
Rocky
  • 129
  • 2
  • 12
0
votes
1 answer

How to pass parameters in correct way from view to controller in Ruby on Rails

I've some time working with Rails but I have a problem and don't know how to solve. There is a model X and a model YW and between them a joint model I need to pass from view to controller as part of the parameters the X the x_yw_attributes but I…
0
votes
1 answer

Is it possible to intertwine Javascript and erb?

I would like to do this, but obviously it's wrong. any way to get the ajax return 'cnut' into the erb? processResults: function (data, params) { // parse the results into the format expected by Select2 // since we are using…
daveasdf
  • 71
  • 7
0
votes
0 answers

Rails form submit button hitting 'new' instead of 'create'

I cannot figure out what is causing my code to hit /new instead of 'create' on submit. I'm using bootstrap_form_with and tried to revert back to just form_with but no luck. Here is my controller: class AccountsController < ApplicationController …
Demian Sims
  • 871
  • 1
  • 14
  • 29
0
votes
1 answer

Missing string after space erb

I have used html.erb template in Rails to passing some value for HTML input and my source code looks like this: <% all = "All value" %> > But when the view is rendered, it seems like a…
Ninh Le
  • 1,291
  • 7
  • 13
0
votes
2 answers

replace variable value on ruby template via puppet

I am looking the way to have the result of this ruby template file: ServerName 1.server.foo knowing that if I run $ facter -p fqdn 1.server.foo.internet.com I would probably play with <%= @fqdn %> and .gsub? server-id: <%= @fqdn…
nerabis
  • 25
  • 1
  • 8
0
votes
1 answer

How to call image_tag and number_to_currency in dynamcially created html from ERB

I am trying to create an html file from ERB, as I want to save a copy of invoice of a very complicated calculation. My ERB template is using many different calculation and using heavily number_to_currency and image_tag Following is my action…
Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
1 2 3
99
100