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.
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.
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!
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.
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.
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>
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
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.
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.
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.
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 %>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With