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 serverThis starts Puma web server on http://localhost:3000.
Creating the Article Model
Generate the Article model with:
rails generate model ArticleThis 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
endRun the migration:
rake db:migrateWorking with the Console
Access the Rails console:
rails consoleTry these commands:
Time.now
Article.all
Article.newCreate and save an article:
a = Article.new
a.title = "Sample Article Title"
a.body = "This is the text for my article!"
a.save
Article.allRouting Configuration
Edit config/routes.rb:
Rails.application.routes.draw do
resources :articles
endView routes with:
rake routesArticles Controller
Generate the controller:
rails generate controller articlesIndex Action
Add to app/controllers/articles_controller.rb:
def index
@articles = Article.all
endCreate 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])
endCreate 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.cssSaving 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 masterTroubleshooting
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.