Ruby on Rails

Rails is a popular web framework that is installed as a Ruby gem. The JavaScript community has npm (the Node package manager) to manage all of your projects dependencies. Ruby uses bundler to manage dependencies, which take the form of gems. These gems are developed by the community and provide extensions and libraries to enhance Ruby's native functionalities. The most popular gem is Rails, a framework for developing robust web applications like Airbnb, Groupon, Github, Hulu, Kickstarter, the list goes on and on. With Rails, developers can build production-ready applications in days or weeks instead of months. One of the reasons Rails provides such a short concept-to-market is the frameworks implementation of "convention over configuration". By coupling the Rails framework and Ruby language, Ruby on Rails has become one of the best options for developing modern database-connected applications.

Embedded Ruby

Rails utilizes an MVC framework (Model, View, Controller) where the views are composed of html with embedded Ruby. These files use the dot ( . ) erb extension, which of course, stands for "embedded ruby". So, for example, an erb file may be named 'about.html.erb'. These files allow developers to add Ruby code to their web pages, which is very useful for variable substitution and flow control.

When composing "*.html.erb" files, any valid HTML can be used. Ruby code needs to be inside <% %> braces to be interpreted correctly. For example,

<%# This is a ruby comment %>

Ruby code between the erb braces <% %> will be executed but nothing will be displayed on the screen. There is a specific syntax to display text on the screen, which is <%= %>. This is an important distinction. Memorize it if you'll be working with the Rails framework in any capacity.

  • <% %> - evaluates Ruby code
  • <%= %> - displays Ruby code

It's common in Rails to store an instance of the current user in a variable and then refer to that variable in the view. For example, refer to the instance as <%= current_user.username %> to print the username to the screen. This allows developers to write something like, "Welcome back, <%= current_user.username %>!" one time in the application yet each user will then see their own username (Note: this assumes there is a field 'username' in the User model).

results matching ""

    No results matching ""