Sure, here’s a simple “Hello World” example in Ruby on Rails:
- 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
- Navigate to the App Directory: Move into the newly created app directory:
cd hello_world_app
- Generate a Controller and View:
- Generate a controller named “welcome” with an action called “index”:
rails generate controller welcome index
- 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>
- Start the Server:
- Run the Rails development server using the following command:
rails server
- 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.