Laragon

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

  1. Open your terminal (Menu > Laragon > Terminal or Ctrl + `)
  2. Run the installation command:
    gem install rails

Creating a New Project

  1. Navigate to your projects folder (e.g., C:\laragon\app)
  2. Create a new Rails application:
    rails new blogger
    cd blogger
  3. 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:

  1. Ensure SQLite3 is installed
  2. Verify the gem is properly installed