Printing elements of array using ERB
Erb, the templating engine you're using in your views, has a few different ways of embedding ruby code inside templates.
When you put code inside <%= %>
blocks, erb evaluates the code inside and prints the value of the last statement in the HTML. Since .each
in ruby returns the collection you iterated over, the loop using <%= %>
attempts to print a string representation of the entire array.
When you put code inside <% %>
blocks, erb just evaluates the code, not printing anything. This allows you to do conditional statements, loops, or modify variables in the view.
You can also remove the puts
from puts t
. Erb knows to try to convert the last value it saw inside <%= %>
into a string for display.
Printing the values of the array in html.erb file in rails
Any time that you write ruby in erb you have to remember to use the correct tag. So change your view to this:
<!DOCTYPE html>
<html>
<head>
<title> Blalalala</title>
</head>
<body>
<h1>Please check the items <br><br></h1>
<%- @lp_array.each do |lp| %>
<%= lp %>
<%- end %>
</body>
</html>
notice that the "=" in the erb tag tells the view to actually show the variable. The "-" in the erb tag tells the view not to show the variable. To print each to a new line you can use <br> or use CSS. for
just add one to the loop like so:
<!DOCTYPE html>
<html>
<head>
<title> Blalalala</title>
</head>
<body>
<h1>Please check the items <br><br></h1>
<%- @lp_array.each do |lp| %>
<%= lp %><br>
<%- end %>
</body>
</html>
Ruby on Rails: Print element of array in a single line in html.erb file
Doesn't something like this work?, just join them, leaving them inside the paragraph tag:
<p class="center">
<%= @names_array.join(', ') %>
</p>
Printing First Photo in Array with Ruby/ERB
<%= image_tag listing.listing_property_details.images.first, class: "fancy-border" %>
ERB: iterate over an array and output each value as part of a one line string
I would try to replace
3 <% @oracle_homes.each do |oracle_home| -%>
4 Cmnd_Alias SQLPLUS = <%= oracle_home -%>/bin/sqlplus
5 Cmnd_Alias SRVCTL = <%= oracle_home -%>/bin/srvctl
6 Cmnd_Alias VOTEDSK = <%= oracle_home -%>/bin/crsctl query css votedisk
7 <% end -%>
with
Cmnd_Alias SQLPLUS = <%= @oracle_homes.map { |path| "#{path}/bin/sqlplus" }.join(', ') %>
Cmnd_Alias SRVCTL = <%= @oracle_homes.map { |path| "#{path}/bin/srvctl" }.join(', ') %>
Cmnd_Alias VOTEDSK = <%= @oracle_homes.map { |path| "#{path}/bin/crsctl query css votedisk" }.join(', ') %>
Printing properties from an array of hashes to an erb in Rails?
Try this:
array.each do |entry|
<h3><a href="#{entry["owner"]["url"]}"> entry["name"] </a></h3>
<div>entry["desc"]</div>
end
rails ul with an array in the view
Make a model called Company
:
#app/models/company.rb
class Company < ActiveRecord::Base
end
You'll also need the corresponding database values:
$ rails g migration CreateCompany
#db/migrate/create_company.rb
class CreateCompany < ActiveRecord::Migration
def change
create_table :companies do |t|
t.string :name
t.timestamps
end
end
end
$ rake db:migrate
--
This will allow you to store Company
records, with corresponding attributes (name
being the one highlighted in this example):
#config/routes.rb
resources :companies #-> url.com/companies
#app/controllers/companies_controller.rb
class CompaniesController < ApplicationController
def index
@companies = Company.all
end
end
#app/views/companies/index.html.erb
<ul class="list-group">
<% @company.each do |company| %>
<%= company.name %>
<% end %>
</ul>
You could create some test Company
values by using the rails console
:
$ rails c
$ names = %w(test1 test2 test3)
$ names.each do |name|
$ - Company.create name: name
$ end
How to render an Array to a YAML list with ERB?
The only problem I see in your example is the missing leading -
from the closing <% end -%>
(should be <%- end -%>
).
# foo.erb:
<%- @arr = [1,2,3] -%>
tags:
<%- @arr.each do |tag| -%>
- <%= tag %>
<%- end -%>
- extra tag
Output:
$ erb -T - foo.erb
tags:
- 1
- 2
- 3
- extra tag
Without the leading -
the result I get is different from yours:
tags:
- 1
- 2
- 3
- extra tag
Read value from an array in rb to an erb file
you could also use a CONSTANT as an alternative like following..
MY_ARRAY = ["apple", "orange", "cherries"]
and it will work:
MY_ARRAY[0] => "apple"
MY_ARRAY.first => "apple"
or simple variable
@my_array = ["apple", "orange", "cherries"]
will also work:
@my_array[0] => "apple"
@my_array.first => "apple"
@my_array.last => "cherries"
Related Topics
Paperclip File Not Found Error
Rails How to Change Attribute Name When Rendering JSON
Grouped Collection Select Alphabetical Order Rails
Xlsx Compressed by Rubyzip Not Readable by Excel
Keyword for Exclusive or in Ruby
Disable Rdoc and Ri Generation by Default for Rubygems 1.8.X
How to Parse a CSV File Located in a Amazon S3 Bucket
Error - "Gem Install Rails" - Libxml2 Is Missing
Open-Uri Returning Ascii-8Bit from Webpage Encoded in Iso-8859
Best Way to Document "Splatted" Parameter with Yard
What Are Symbols and How Do We Use Them
How to Render a String as an Erb File
Parsing Date from Text Using Ruby
Getting Current Line of Code in Ruby
How to Add a Custom Log Level to Logger in Ruby
How to Require File from 'Gem' Which Are Not Under 'Lib' Directory