How to Run Multiple Lines of Ruby in HTML.Erb File

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



Leave a reply



Submit