Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run multiple lines of Ruby in html.erb file

I'm using Ruby on Rails and need to run a block of Ruby code in one of my html.erb files. Do I do it like this:

<% def name %>
<% name = username %>
<%= name %>

or like this:

<% def name
name = username %>
<%= name %>

Thanks for reading.

like image 528
ben Avatar asked Jun 23 '10 08:06

ben


People also ask

How do I view an ERB file in HTML?

View templates We will use HTML tags in Rails. HTML tags provide static web pages only but ERB tags give us dynamic information in that HTML template. To view the template file, go to Rails application >> app >> View>> Home folder where the templates files are available.

How do you comment lines in an ERB file?

To comment one line I must use 3 additional characters, and the block comment is nothing but code that will be not executed - no other color coding that makes it very unpractical to see which code is not executed on first look. @gotqn Then you will LOVE HAML!

What does ERB do in Ruby?

ERB (or Ruby code generated by ERB) returns a string in the same character encoding as the input string. When the input string has a magic comment, however, it returns a string in the encoding specified by the magic comment.

What is HTML ERB file?

As @Chowlett mentioned before, erb stands for Embedded Ruby. When you define any file as ". html. erb" that means it's an HTML file with ruby code embedded in it and it is similar to ". rhtml" extension of rails file.

What is HTML ERB in Ruby?

HTML.ERB is HTML mixed with Ruby, using HTML tags. All of Ruby is available for programming along with HTML. <% %> # executes the Ruby code <%= %> # executes the Ruby code and displays the result <ul> <% @products.each do |p| %> <li><%= @p.name %></li> <% end %> </ul>

How to use Ruby with HTML?

HTML.ERB is HTML mixed with Ruby, using HTML tags. All of Ruby is available for programming along with HTML. Following is the syntax of using Ruby with HTML − <%%> # executes the Ruby code <%=%> # executes the Ruby code and displays the result

Is Ruby code written in one file or multiple files?

If you look at any open source Ruby projects online, you'll quickly notice that their code isn't all written in one file. In fact, programmers generally try to reduce the length of the files they are writing by splitting their code up into multiple files.

How do I add Ruby code to a plain text document?

Using ERB, actual Ruby code can be added to any plain text document for the purposes of generating document information details and/or flow control. require 'erb' x = 42 template = ERB. new <<-EOF The value of x is: <%= x %> EOF puts template. result ( binding ) More complex examples are given below.


2 Answers

If you need extra functions in your view, you normally declare those inside a helper.

For each controller, if there is a helper it is automatically loaded. For instance, if you have a PeopleController, in the app/helpers folder, there should be a people_helper.rb, and it should look like this

module PeopleHelper   def name     #do something     username   end end 

Another, very clean alternative, is to use the Presenter pattern, but i think it is less common (unfortunately).

Otherwise, if you do need multiple lines of ruby code inside a erb view, which i try to avoid, i prefer the following style:

<%    counter_1 = 0    counter_2 = 1    do_some_more_prep_here  %> <% @records.each do |rec|%>   <%# do something with the prepped date in each row %> <% end %> 

Also for me code indentation is more important than html indentation, so i will prefer something like

<table>    <% @rows.each do |row| %>     <tr>       <td><%= row.item1 %></td>       <% if row.some_test %>         <td><%= row.item2 %></td>       <% end %>     </tr>   <% end %> </table> 

But i am always very interested to hear different opinions in this matter.

like image 122
nathanvda Avatar answered Oct 08 '22 09:10

nathanvda


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 %>.

like image 29
shingara Avatar answered Oct 08 '22 10:10

shingara