Up and Running
Learn how to quickly install Rails, generate your first app, configure the database, and launch your local server
Introduction
Ruby on Rails became popular quickly because it handles much of the hard work for developers, especially when starting a new project. Rails follows the principle of "sensible defaults" - with just one command, you can create a working application ready for customization.
Setting Up a Rails Project
Installing Rails
- Open your terminal (Menu > Laragon > Terminal or Ctrl + `)
- Run the installation command:
gem install rails
Creating a New Project
- Navigate to your projects folder (e.g.,
C:\laragon\app
) - Create a new Rails application:
rails new blogger cd blogger
- Open the project in your text editor
Project Structure Overview
The Rails generator creates this folder structure:
- app: Contains your application code (Models, Controllers, Views, Helpers, JavaScript)
- bin: Stores executables (
bundle
,rails
,rake
,spring
) - config: Environment settings and initializers
- db: Database migrations and SQLite3 database file
- lib: Reusable code outside the project
- log: Environment-specific log files
- public: Static files
- test: Test files (when using Test::Unit)
- tmp: Temporary cached files
- vendor: Third-party code
Database Configuration
The database settings are in config/database.yml
. For SQLite3 (Rails default), the configuration is pre-set to create db/development.sqlite3
.
Starting the Server
rails server
This starts Puma web server on http://localhost:3000
.
Creating the Article Model
Generate the Article model with:
rails generate model Article
This creates:
- Migration file in
db/migrate
- Model file in
app/models/article.rb
- Test files
Migration File
Edit db/migrate/[timestamp]_create_articles.rb
:
def change
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
Run the migration:
rake db:migrate
Working with the Console
Access the Rails console:
rails console
Try these commands:
Time.now
Article.all
Article.new
Create and save an article:
a = Article.new
a.title = "Sample Article Title"
a.body = "This is the text for my article!"
a.save
Article.all
Routing Configuration
Edit config/routes.rb
:
Rails.application.routes.draw do
resources :articles
end
View routes with:
rake routes
Articles Controller
Generate the controller:
rails generate controller articles
Index Action
Add to app/controllers/articles_controller.rb
:
def index
@articles = Article.all
end
Create app/views/articles/index.html.erb
:
<h1>All Articles</h1>
<ul id="articles">
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article_path(article), class: 'article_title' %>
</li>
<% end %>
</ul>
<%= link_to "Create a New Article", new_article_path, class: "new_article" %>
Show Action
Add to the controller:
def show
@article = Article.find(params[:id])
end
Create app/views/articles/show.html.erb
:
<h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<%= link_to "<< Back to Articles List", articles_path %>
Styling
Download the CSS file:
cd app/assets/stylesheets/
wget http://tutorials.jumpstartlab.com/assets/blogger/screen.css
Saving to GitHub
Initialize a Git repository:
git init
git add .
git commit -m "first blogger commit"
git remote add origin https://github.com/your_username/your_repo.git
git push -u origin master
Troubleshooting
If you see database errors:
- Ensure SQLite3 is installed
- Verify the gem is properly installed
Environment Setup
Laragon is the best choice for running Ruby on Rails on Windows, offering a reliable, portable, and fully-featured development environment.
Form-based Workflow
This guide covers setting up the new action, building the form with form_for, handling validation errors, and connecting it all to your model.