Introduction
Ruby on Rails (RoR) is a powerful web development framework that makes building web applications easy and efficient. Whether you’re a beginner or an experienced developer, Rails provides a structured way to create scalable applications. In this guide, we’ll walk through the basics of Ruby on Rails and how to set up your first Rails project.
What is Ruby on Rails?
Ruby on Rails is an open-source web application framework written in Ruby. It follows the MVC (Model-View-Controller) architecture, making it easy to separate logic, design, and data management.
Why Choose Ruby on Rails?
Rapid Development: Rails comes with built-in tools that speed up coding.
Convention Over Configuration: You don’t have to write a lot of boilerplate code.
Scalability: Used by companies like GitHub, Shopify, and Airbnb.
Active Community: Thousands of developers contribute to Rails, making it easy to find support.
Installing Ruby on Rails
To get started, you need to install:
- Ruby – The programming language.
- Rails – The framework.
- A Database – SQLite (default), PostgreSQL, or MySQL.
Step 1: Install Ruby
Run the following command to install Ruby using RVM (Recommended for macOS/Linux):
\curl -sSL https://get.rvm.io | bash -s stable
rvm install ruby
rvm use ruby --default
ruby -v
Step 2: Install Rails
gem install rails
rails -v
Step 3: Create a New Rails Project
rails new my_first_app
cd my_first_app
Step 4: Run the Rails Server
rails server
Now, open http://localhost:3000 in your browser. You should see the Rails welcome page!
Understanding the Rails Folder Structure
app/
– Contains the core application code (models, views, controllers).
config/
– Stores configuration files.
db/
– Database-related files.
public/
– Static files like images, CSS, and JavaScript.
Gemfile
– Specifies dependencies for your project.
Creating Your First Controller and View
To generate a controller, run:
rails generate controller Home index
This creates a HomeController with an index
action. Now, modify config/routes.rb
:
Rails.application.routes.draw do
root "home#index"
end
Now, open app/views/home/index.html.erb and add:
<h1>Welcome to My First Rails App!</h1>
Restart your server and visit http://localhost:3000 to see your homepage.
Conclusion
You’ve successfully set up a basic Rails project! From here, you can explore models, controllers, views, and databases. Stay tuned for more Rails information!