Rails Tags

There are two variations on the Rails tags that you can place into Rails HTML Embedded Ruby (ERb) template files. The ones you’ve used so far include an equal sign in the opening delimiter: <%=.

These tags cause Rails not only to evaluate Ruby expressions but also to display the results in a web page. If you omit the equals sign from the opening delimiter, then the code will be evaluated, but the result will not be displayed: <%.

Note

ERb files contain a mix of HTML markup and Ruby code between tags such as <%= and %>. Rails processes these files before the final pages are displayed in the web browser, executing the embedded Ruby and constructing the HTML page as a result.

If you want, you can place quite long pieces of code—your entire Ruby program even!—between <% and %> tags and then use <%= and %> when you want to display something in the web page. In fact, you could rewrite your application by omitting the Controller entirely and putting everything into the View. Try it by editing app/views/say_hello/index.html.erb to match the following (or copy and paste the code from the file embed_ruby_rails2.html.erb or embed_ruby_rails3.html.erb according to the Rails version being used):

embed_ruby_rails3.rhtml

<% def showFamily( aClass, msg )
     if (aClass != nil) then
        msg += "<li>#{aClass}</li>"
        showFamily( aClass.superclass, msg )
     else
       return msg
     end
   end %>

<%= raw( "<ul>#{showFamily( self.class, "" )}</ul>" ) %>

In this particular case, the text displayed in the web page will be slightly different from before since it now shows the class hierarchy of the view’s class, rather than that of the controller’s class. As you will see, the view descends from the ActionView::Base class.

You can also divide a contiguous block of code by placing the individual lines between <% and %> tags instead of placing the entire block between a single pair. The advantage of doing this is that it lets you put standard HTML tags outside the individually delimited lines of Ruby code. You could, for instance, put this into a view:

<% arr = ['h','e','l','l','o',' ','w','o','r','l','d'] %>

<% # sort descending from upper value down to nil
reverse_sorted_arr = arr.sort{
    |a,b|
        b.to_s <=> a.to_s
    } %>

<% i = 1 %>
<ul>
<% reverse_sorted_arr.each{ |item| %>
<li><%= "Item [#{i}] = #{item}" %></li>
<% i += 1 %>
<% } %>
</ul>

Here, I’ve assigned an array of chars to the variable, arr, between one set of tags. I’ve written a block to reverse-sort the array and assigned the result to another variable between a second set of tags. Then I’ve assigned 1 to the variable, i; and finally I’ve written this method:

reverse_sorted_arr.each{ |item|
    "Item [#{i}] = #{item}"
    i += 1
}

But instead of enclosing the method between a single set of <% %> tags, I’ve enclosed each separate line within its own pair of tags. Why should I do this? Well, there are two reasons. First, I want the string in the middle of the block to be displayed on the web page, so I need to use the <%= tag there:

<%= "Item [#{i}] = #{item}" %>

And second, I want the whole set of strings to be displayed as an HTML list. So, I’ve placed the <ul> and </ul> tags before and after the Ruby code block, and I’ve placed the line of code that displays each array item inside <li> and </li> tags. Notice that these tags are inside the Ruby code block but outside the embedded Ruby tags on this particular line:

<li><%= "Item [#{i}] = #{item}" %></li>

So, by dividing up a contiguous block of Ruby code into separately delimited lines, I am no longer forced to construct strings to contain HTML tags. Instead, I have been able to do the useful trick of mixing HTML into the Ruby code itself. To be honest, I haven’t really mixed it in at all—the Ruby code is still closed off inside the tags; what I’ve done is told Rails to mix in the HTML at specific points prior to displaying the page in a web browser.

Incidentally, you may find it interesting to compare the version of the application that puts all the embedded Ruby code into the view (index.html.erb) with the previous version in which the code was all put into the Controller (the version of say_hello_controller.rb supplied in the sample file sayhello2.rb) and only tiny bits of embedded Ruby (a couple of variables) were placed into the view:

<%= @heading %>
<%= @class_hierarchy %>

You will probably agree that the first version, in which the programming logic was put into the Controller rather than embedded into the View, is neater. On the whole, Ruby code belongs in Ruby code files, and HTML formatting belongs in HTML files. Although embedded Ruby provides an easy way of letting a View and a Controller communicate, it is generally better to keep embedded Ruby code short and simple and put more complex Ruby code into Ruby code files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.67.27