The Pragmatic Programmer

Appian, Pega, Java, DevSecOps, Quality Engineering

A simple “Hello World” example in Ruby on Rails

Sure, here’s a simple “Hello World” example in Ruby on Rails:

  1. Create a New Rails Application: Open your terminal and navigate to the directory where you want to create your Rails application. Run the following command to create a new Rails app named “hello_world_app”:
rails new hello_world_app
  1. Navigate to the App Directory: Move into the newly created app directory:
cd hello_world_app
  1. Generate a Controller and View:
  • Generate a controller named “welcome” with an action called “index”:
rails generate controller welcome index
  1. Edit the View:
  • Open the file located at app/views/welcome/index.html.erb using a text editor, and replace the content with:
<!DOCTYPE html>
<html>
<head>
  <title>Hello World</title>
</head>
<body>
  <h1>Hello, Rails!</h1>
  <p>Welcome to the world of Ruby on Rails.</p>
</body>
</html>
  1. Start the Server:
  • Run the Rails development server using the following command:
rails server
  1. View the Hello World Page:
  • Open your web browser and go to http://localhost:3000. You should see the “Hello, Rails!” message displayed on the page.

Congratulations! You’ve successfully created a “Hello World” example using Ruby on Rails. This is a very basic example, but it gives you a taste of how controllers, views, and routes work together in a Rails application. From here, you can explore more advanced features and build more complex applications.