Ruby: Ruby on Rails quick start
Getting started with Ruby on Rails
👋 Welcome to the Stackhero documentation!
Stackhero provides a ready-to-use Ruby cloud solution offering a range of advantages, including:
- Deploy your application within seconds using a simple
git push.- Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
- Take advantage of automatic backups, one-click updates, and clear, transparent, and predictable pricing.
- Enjoy optimal performance and enhanced security thanks to a private, dedicated infrastructure.
Save time and make your life easier: it only takes 5 minutes to try Stackhero's Ruby cloud hosting solution!
Welcome to the quick start guide for Ruby on Rails. This guide introduces you to one of the most popular web application frameworks, designed to simplify and speed up your development process.
Ruby on Rails (often referred to simply as Rails) is a server-side web application framework built with Ruby. It follows the model-view-controller (MVC) architectural pattern and adopts the Convention over Configuration (CoC) and Don't Repeat Yourself (DRY) principles. These features make Rails a powerful, efficient, and flexible solution for web development.
Understanding the difference between Ruby and Rails
Ruby is a dynamic, open-source programming language, similar to PHP, JavaScript, or Python. It emphasises simplicity and productivity, with elegant syntax that is easy to read and write.
Rails is a robust server-side web application framework that runs on Ruby. It provides a structured, standardised approach to building web applications by offering tools and libraries that simplify common development tasks.
Many developers choose Ruby on Rails over using Ruby alone because its comprehensive framework accelerates development and improves productivity.
Creating a Ruby project
Starting a new Ruby project is straightforward. Follow these steps to get started:
-
First, ensure you have the latest version of Ruby installed on your system. You can check this by running the following command in your terminal:
asdf install ruby latest \ && asdf global ruby latestIf you have not installed
asdfyet, you will need to do so before continuing. Please refer to the official asdf documentation for detailed installation instructions. -
Next, install Rails. Rails is distributed as a RubyGem, which makes installation very straightforward:
gem install rails -
Then, create a new Rails application named
my_appand specify the Ruby version with these commands:rails new my_app cd my_app asdf local ruby latest -
To start the Rails server, run the following command:
bin/rails server
Ruby on Rails welcome screen
These steps create a new Ruby project while setting the appropriate Ruby version. This provides a solid foundation for your application development.
Important: When you run the
rails newcommand, a local Git repository is automatically created for your Ruby project. However, it is not yet connected to a remote version control service.For proper version control and data security, it is recommended to connect your project to a hosting service such as GitLab, GitHub, or Bitbucket.
If you need a repository hosting service, Stackhero offers a GitLab cloud service that can be set up in just 2 minutes.
Creating your first "hello world" application with Ruby on Rails
Let's go through how to create a simple "Hello World" application with Ruby on Rails.
-
Start by defining a new route for your application. Open the
config/routes.rbfile and add a root route that points to thehello#indexaction:Rails.application.routes.draw do root "hello#index" end -
Next, generate the
Hellocontroller to handle the logic for this route. Run the following command in your terminal:bin/rails generate controller Hello index --skip-routesWith this setup, any request to the URL path
/will be handled by theHelloController. -
To display the "Hello World" message, create a view. Open the file at
app/views/hello/index.html.erband replace its content with the following code:<h1>Hello World!</h1> -
Finally, start the Rails server by running the command below in your terminal. Then, connect to your development URL (for example,
http://<XXXXXX>.stackhero-network.com:3000) to view your application:bin/rails server -b 0.0.0.0
First "Hello World" page in Ruby on Rails
Congratulations. You have just created your first Ruby on Rails application!
Managing environment variables
Environment variables are essential for protecting sensitive data such as API keys and database passwords.
The dotenv-rails gem provides a convenient way to manage environment variables in your development environment. Here is how to proceed:
-
Add the gem to your Gemfile in the appropriate groups:
# Gemfile gem 'dotenv-rails', groups: [:development, :test] -
Install the gem by running:
bundle install -
Create a
.envfile at the root of your project and define your environment variables. For example:RAILS_ENV=development -
To protect your sensitive data, make sure the
.envfile is excluded from your Git repository. Add the following line to your.gitignore:echo '.env*' >> .gitignore
After completing these steps, restart your Rails server. The environment variables defined in the .env file will then be automatically accessible in your application.
Environment variables for your development platform