BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Embedded Ruby (ERb) and RHTML 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:




Embedded Ruby (ERb)

In Rails, for each action in each controller, there is an associated view source file, called a template. The view is responsible for the presentation of the page. If we want to have a good Model-View-Controller design practices, view code should include everything related to generating the associated HTML output, but not incorporate business logic, direct access to the database, nor generally any direct access to model objects. All of the data it needs is prepared for it by the controller and placed into variables for use by the view.

Rails can render views in various ways. Here, we'll use the most common one, in which the template is called an RHTML file, for Ruby HTML, because it is a mix of both.

When Rails processes a template file, how does rails tell the difference between HTML code that it should just send to the browser and Ruby code that it needs to execute? RHTML solves this by providing hints in the form of special tags to identify pieces of Ruby code.

Ruby code in a view template is enclosed by <% %>. The enclosed text is what is called embedded Ruby or ERb.

However, there's one slight subtlety. Sometimes we may want to write code that provides output to be displayed, and sometimes we have some simple, yet invisible logic needed to construct the page, such as a loop.

If the Ruby code is preceded by just <%, then the code is executed, but its output is NOT inserted into the HTML stream. We'd use this in our example of a loop or a conditional, for instance. So, we might have Ruby code that says something like “if post title is not blank”. In real code, this would be:

<% if !post.title.blank? %>

The code doesn't produce any output, instead, this statement determines whether or not the lines that follow it will be used or not.

If we want to have the output from the Ruby code inserted into the HTML stream, we add an equal sign after the opening tag, so the symbols that start the embedded Ruby block are less-than symbol, percent, equal sign (<%=). The equal sign indicates that the output of this Ruby code is to be used as HTML.

if we had a variable named title and we wanted its value to be inserted into the HTML, we do this:

<%= title %>

So:

  1. <% and %> wrap Ruby code whose return value will NOT be output.
  2. <%= and %> wrap Ruby code whose return value will be output in place of the marker.
  3. <% and -%> The extra dash makes ERB not output the newline after the closing tag. So,
    <div>
      <% if true -%>
      Ruby
      <% end -%>
    </div>
    
    will produce:
    <div>
    
      Ruby
    
    </div>
    




haml / slim

There are other alternatives to erb:

An Analysis of the State of Haml vs Slim : ruby - Reddit.

For haml with erb - visit Templating with ERB & HAML.









Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Ruby on Rails



Ruby On Rails Home

Ruby - Input/Output, Objects, Load

Ruby - Condition (if), Operators (comparison/logical) & case statement

Ruby - loop, while, until, for, each, (..)

Ruby - Functions

Ruby - Exceptions (raise/rescue)

Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)

Ruby - Class and Instance Variables

Ruby - Class and Instance Variables II

Ruby - Modules

Ruby - Iterator : each

Ruby - Symbols (:)

Ruby - Hashes (aka associative arrays, maps, or dictionaries)

Ruby - Arrays

Ruby - Enumerables

Ruby - Filess

Ruby - code blocks and yield

Rails - Embedded Ruby (ERb) and Rails html

Rails - Partial template

Rails - HTML Helpers (link_to, imag_tag, and form_for)

Layouts and Rendering I - yield, content_for, content_for?

Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag

Rails Project

Rails - Hello World

Rails - MVC and ActionController

Rails - Parameters (hash, array, JSON, routing, and strong parameter)

Filters and controller actions - before_action, skip_before_action

The simplest app - Rails default page on a Shared Host

Redmine Install on a Shared Host

Git and BitBucket

Deploying Rails 4 to Heroku

Scaffold: A quickest way of building a blog with posts and comments

Databases and migration

Active Record

Microblog 1

Microblog 2

Microblog 3 (Users resource)

Microblog 4 (Microposts resource I)

Microblog 5 (Microposts resource II)

Simple_app I - rails html pages

Simple_app II - TDD (Home/Help page)

Simple_app III - TDD (About page)

Simple_app IV - TDD (Dynamic Pages)

Simple_app V - TDD (Dynamic Pages - Embedded Ruby)

Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)

App : Facebook and Twitter Authentication using Omniauth oauth2

Authentication and sending confirmation email using Devise

Adding custom fields to Devise User model and Customization

Devise Customization 2. views/users

Rails Heroku Deploy - Authentication and sending confirmation email using Devise

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II

OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master