DEV Community

Marc West
Marc West

Posted on

Ruby on Rails

Ruby is an object-oriented, coding language first released in 1995 by Yukiro Matsumoto. It was specially designed with a focus on productivity and simplicity. Some of its major selling points are that Ruby does not take long to learn, and it is open source so it can be modified according to the user’s specific needs. Although it was largely developed on Linux, it functions across all platforms, including Mac OS and Windows.

Built on the Ruby programming language, Ruby on Rails is a web framework that comes with everything you need to complete a website project out of the box - including the ability to manage logic and routing and handle applications. These useful tools also include database integration and controllers which makes it easier to integrate web development from front to back.

To get started with a rails application, you first need to install Ruby and Rails. After a lot of searching I came across this article which seemed to have all the steps I needed to get them running on Linux Mint 19.3. Once installed you can run the command rails new [app name] to generate a new application with all the files needed to get started including the Gemfile. The Gemfile is much like Node.js' package.json, and similarly it also has an install command which needs to be run, bundle install, to download dependencies. After that enter in rails s or rails server to run the built-in development server and you will be greeted in localhost:3000
Alt Text

A controller handles communication between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates user data to the model. A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action. To create a controller enter rails generate controller [ControllerName]. This will generate a controller file, helper file, and view folder for your controller as well as a few other helpful files like stylesheets. Functions in Ruby are declared with def followed by the function name and closed off with end. This function will determine what data is relayed to the about.html.erb page.
Alt Text

The information on the view is dynamically bound to the variables @title and @content

<h1><%= @title %></h1>
<p><%= @content %></p>

about.html.erb

To produce the about route go to routes.rb inside the config file. In there we need to make a get request to get the pages controller at the about view. That can be achieved with this syntax get 'about' => 'pages#about', as: 'about'
Alt Text
The final product will look like this
Alt Text

To create a model you can run rails g [Model]. This will create a model and also a migration file located in db/migrate. Run the migration file by entering rake db:migrate in the cli. The migrate file will be the template, in this example a post, of what will be saved to the database by the model.
Alt Text

Top comments (0)