How do I run multiple lines of Ruby in html.erb file
It is unusual to define a method in an ERB file, so I recommend against it.
If you want to call a block like #each
, you can do something like the following:
<% names.each do |name| %>
<%= name %>
<% end %>
Don't forget the <% end %>
.
Ruby on Rails - Creating rows in an html.erb table by looping over instance variable
By default in ERB, code blocks are enclosed in <%
and %>
delimiters, and if you want the result of the code block run, you use the =
symbol inside, like <%= 'Hel' + 'lo' %>, world
would output "Hello, world".
Each member being iterated over with .each
is named between the pipe symbols, so that is what you reference inside the block.
Also table rows need to have table data, or <td>
tags within the row <tr>
tags.
Here's what should work for you assuming that @referrals
is a list of strings:
<table style="border:1px solid black;margin-left:auto;margin-right:auto;">
<tr> <th> Referral Emails </th> </tr>
<% @referrals.each do |referral| %>
<tr> <td> <%= referral %> </td> </tr>
<% end %>
</table>
Block comments in html.erb templates in rails
I wouldn't count as a solution, but perhaps enclosing the chunk between an
<% if false %>
...
<% end %>
or if you feel a little dirty, create a helper that simply outputs nothing.
I've never needed it, but I'm stumbled there seems to be no out-of-the-box solution for this.
How to comment lines in rails html.erb files?
ruby on rails notes has a very nice blogpost about commenting in erb-files
the short version is
to comment a single line use
<%# commented line %>
to comment a whole block use a if false
to surrond your code like this
<% if false %>
code to comment
<% end %>
MultiLine Html Code in js file :Ruby on Rails
use a partial and pass @comments
as a collection
$('div.showcomment').html('<%= escape_javascript render(partial: 'comments/comment', collection: @comments, locals: { article: @article } %>')
# app/views/comments/_comment.html.erb
<%= comment.body %>
<%= link_to 'Delete Comment', articles_deletecomment_path(id: comment.id, article_id: article.id), method: :delete, remote: true, data: { confirm: 'Are you sure?' } %>
<a onclick="editComment(<%= comment.id%>)">Edit Comment</a>
<br>
Assign a string of html.erb or multiple line with .val() in Rails js.erb
Just use j
(alias for escape_javascript
):
$('#argument_value').val("<%= j(arg_value) %>");
Related Topics
Rake: Logging Any Task Executing
Error When Installing Ruby 2.1.3 with Rvm
Single Custom Param Name in Routes for Nested Resources Rails 4.1
What Are Factory_Girl Transient Attributes? Why Would I Use One
Should Rbenv Be Installed System-Wide, or at a User Level
Linux Cli: How to Render Arabic Text into Bitmap
Anything Speaking Against the Bitnami.Org Ruby/Rails/Redmine Stack
Robustly Call a Flaky API: Proper Error Handling with Net::Http
Installing Pl/Ruby for Postgresql 8.3
Ruby/Rails Collection to Collection
Form_For with Multiple Controller Actions for Submit
Thin Doesn't Respond to Sigint or Sigterm
Truncate a Floating Point Number Without Rounding Up
Why Does Using '-Webkit-Appearance: None' on a Select Option in Osx Make The Text Disappear
What Are Some Examples of Using Nokogiri
Rails App Moved to Production Server Gives "Dump Format Error for Symbol" Error