Ruby: Ruby on Rails quickstart

Getting started with Ruby on Rails

👋 Welcome to the Stackhero documentation!

Stackhero provides a ready-to-use Ruby cloud solution that delivers numerous advantages, including:

  • Deploy your application in just a few seconds with a simple git push.
  • Use your own domain name and take advantage of automatic configuration of HTTPS certificates for enhanced security.
  • Benefit from automatic backups, one-click updates, and clear, transparent, and predictable pricing.
  • Enjoy optimal performance and enhanced security thanks to a private, dedicated VM.

Save time and make your life easier: it only takes 5 minutes to try Stackhero's Ruby cloud hosting solution!

Welcome to the Ruby on Rails quickstart guide. 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 simply called Rails) is a server-side web application framework built with Ruby. It follows the model-view-controller (MVC) architectural pattern and applies 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.

Ruby is a dynamic, open-source programming language, similar to PHP, JavaScript, or Python. It focuses on 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 and standardized 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.

Starting a new Ruby project is straightforward. Follow these steps to get started:

  1. First, make sure 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 latest
    

    If you have not installed asdf yet, you will need to do so before continuing. Please refer to the official asdf documentation for detailed installation instructions.

  2. Next, install Rails. Rails is distributed as a RubyGem, which makes installation very straightforward:

    gem install rails
    
  3. Then, create a new Rails application named my_app and specify the Ruby version with these commands:

    rails new my_app
    cd my_app
    asdf local ruby latest
    
  4. To launch the Rails server, run the following command:

    bin/rails server
    

    Ruby on Rails welcome screenRuby on Rails welcome screen

These steps create a new Ruby project while setting the appropriate Ruby version. This gives you a solid foundation for developing your application.

Important: When you run the rails new command, 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 safety, 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.

Let's walk through how to create a simple "Hello World" application using Ruby on Rails.

  1. Start by defining a new route for your application. Open the config/routes.rb file and add a root route that points to the hello#index action:

    Rails.application.routes.draw do
      root "hello#index"
    end
    
  2. Next, generate the Hello controller to handle the logic for this route. Run the following command in your terminal:

    bin/rails generate controller Hello index --skip-routes
    

    With this setup, any request to the URL path / will be handled by the HelloController.

  3. To display the "Hello World" message, create a view. Open the file at app/views/hello/index.html.erb and replace its content with the following code:

    <h1>Hello World!</h1>
    
  4. 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 RailsFirst "Hello World" page in Ruby on Rails

Congratulations. You have just created your first Ruby on Rails application!

Environment variables are essential for protecting sensitive data such as API keys or database passwords.

The dotenv-rails gem provides a convenient way to manage environment variables in your development environment. Here’s how to proceed:

  1. Add the gem to your Gemfile in the appropriate groups:

    # Gemfile
    gem 'dotenv-rails', groups: [:development, :test]
    
  2. Install the gem by running:

    bundle install
    
  3. Create a .env file at the root of your project and define your environment variables. For example:

    RAILS_ENV=development
    
  4. To protect your sensitive data, make sure the .env file 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 platformEnvironment variables for your development platform