Ruby on Rails (often shortened to Rails) is a server-side web application framework written in the Ruby programming language. It is a popular choice for developing web applications because it follows the "Convention over Configuration" principle, meaning that it provides sensible defaults and conventions that make development faster and easier.
Rails is known for its:
Rails requires Ruby to run. You can download and install Ruby from the official website: https://www.ruby-lang.org/
Once Ruby is installed, you can install Rails using the following command:
gem install rails
To create a new Rails project, use the following command:
rails new my_project
Replace "my_project" with your desired project name.
Navigate to your project directory and start the development server with the following command:
rails server
The server will start running on port 3000 by default. You can access your application by opening your browser and navigating to http://localhost:3000/.
Models represent the data in your application. They interact with the database to store and retrieve information. In Rails, models are typically defined in the app/models
directory.
# app/models/user.rb
class User < ApplicationRecord
has_many :posts
end
Controllers handle requests from the browser and interact with models to retrieve or update data. They then render views to display the results to the user. Controllers are defined in the app/controllers
directory.
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all
end
end
Views are responsible for rendering the HTML that the user sees. They use templates and helper methods to generate dynamic content. Views are located in the app/views
directory.
# app/views/posts/index.html.erb
Posts
<% @posts.each do |post| %>
- <%= post.title %>
<% end %>
Routes define how URLs map to controllers and actions. They are specified in the config/routes.rb
file.
# config/routes.rb
Rails.application.routes.draw do
resources :posts
end
This route definition maps the URL /posts
to the PostsController
and its index
action.
Let's create a simple blog application to demonstrate the basic concepts of Rails.
rails generate model Post title:string content:text
This command generates a Post
model with title
and content
attributes.
rails generate controller Posts
This generates a PostsController
with basic actions (index
, new
, create
, show
, edit
, update
, destroy
).
In config/routes.rb
, ensure the generated resource route is present:
Rails.application.routes.draw do
resources :posts
end
Create the following views in app/views/posts
:
# app/views/posts/index.html.erb
Blog Posts
<% @posts.each do |post| %>
- <%= link_to post.title, post_path(post) %>
<% end %>
# app/views/posts/show.html.erb
<%= @post.title %>
<%= @post.content %>
# app/views/posts/new.html.erb
<%= form_with model: @post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit "Create Post" %>
<% end %>
# app/views/posts/edit.html.erb
<%= form_with model: @post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit "Update Post" %>
<% end %>
Start the development server (rails server
) and access the application in your browser. You should now be able to create, view, edit, and delete blog posts.