Table of Contents

A sample Rails application - mydiary

Starting with a blank rails app

cd work_dir # where work_dir is a directory of your choice

mkdir rails # create rails directory

cd rails

rails new mydiary # This rails command will create a mydiary project directory.

cd mydiary

Database (SQLite3) Config

Using "Scaffolding"

rails generate scaffold Entry title:string content:text

Migrations to create db tables

# .rb script in the db/migrate directory of the rails project
#
class CreateEntries < ActiveRecord::Migration[8.0]
  def change
    create_table :entries do |t|
      t.string :title
      t.text :content

      t.timestamps
    end
  end
end
### Recommended in rails 4+ (bundle forces use of the rake version specified in Gemfile) ###
bundle exec rake db:migrate

### If you don't have the 'bundle' command after installing rails (Windows), then just run
###
###      rake db:migrate

Configure web app access host authorization (SP23, Rails 6+)

# Authorize access from any host:
config.hosts.clear

Start simple web server and view current web app

rails server -b 0.0.0.0 -p <port>
   ### <port> = some value >= 3000 ###
http://localhost:<your chosen port>/entries  # e.g., http://localhost:3010/entries

Controllers

Views and Embedded Ruby

<%### _entry.html.erb (partial) ###%>
<div id="<%= dom_id entry %>">
  <p>
    <strong>Title:</strong>
    <%= entry.title %>
  </p>

  <p>
    <strong>Content:</strong>
    <%= entry.content %>
  </p>

</div>




<%### show.html.erb ###%>
<p style="color: green"><%= notice %></p>

<%### Use _entry.html.erb partial ###%>
<%= render @entry %>

<div>
  <%= link_to "Edit this entry", edit_entry_path(@entry) %> |
  <%= link_to "Back to entries", entries_path %>

  <%= button_to "Destroy this entry", @entry, method: :delete %>
</div>

Creating a New Action and View

# Defines a new method and, therefore, a new controller action.
# Similar to index, but sorted in descending (DESC) order
# by creation time.
# GET /entries/view_all
def view_all
    @entries = Entry.order('created_at DESC').all
end
resources :entries
# Redefining root route as the view_all action:
root 'entries#view_all'

# Defining this route to use new view_all controller action:
get '/entries/view_all' => 'entries#view_all'
<%# view_all.html.erb: Define the view for the view_all controller action. %>

<%= link_to 'Add New entry', controller: 'entries', action: 'new' %>

<hr></hr>

<% @entries.each do |entry| %>
    <h3><%= entry.title %></h3>
    <p><%= entry.content %></p>
    <p><em>Posted at <%= entry.created_at %></em></p>
    <p><%= link_to "Show this entry", entry %></p>
    <hr></hr>
<% end %>
rails routes

Modify Views

<%# index.html.erb: Tabular index view with all actions %>

<h1>Listing Entries</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Content</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @entries.each do |entry| %>
      <tr>
        <td><%= entry.title %></td>
        <td><%= entry.content %></td> 
        <td><%= link_to 'Show', entry %></td>
        <td><%= link_to 'Edit', edit_entry_path(entry) %></td>
        <td><%= button_to 'Delete', entry, method: :delete, data: { turbo_confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Entry', new_entry_path %>

Add Users and Authentication (SP26)

There are some choices for adding users and authentication to a Rails app. The method used here will be the devise gem. Sources used are the following:

1. Add the 'devise' gem to the mydiary project.

Run the following command in a terminal in the project root folder:

bundle add devise

This will modify the project Gemfile and install the devise gem.

2. Set up 'devise' gem:

rails g devise:install  # 'g' is short for 'generate'

3. Configure Devise:

Define required default url option in your environment file. Edit the file config/environments/development.rb and add the line line:

config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

before the end keyword.

4. Setup the User model

Create the User model:

rails g devise user		# See generated migration script under 'db/migrate'
                                # and 'config/routes.rb'.
                                
rails db:migrate                # Update project sqlite3 db with the 'users' table.

'Devise' creates all code and routes required to create accounts, log in, log out, etc.

5. Create first user

Start the rails server as before

rails server -b 0.0.0.0 -p 3000

Browse http://localhost:3000/users/sign_up and create a user account.

6. Add sign-up and login links

Edit the files app/views/entries/show.html.erb and app/views/entries/index.html.erb, and remove (or comment out) any line that looks like the following:

<p style="color: green"><%= notice %></p>

Add a notice about the user being logged and a navigation bar containing appropriate links by editing app/views/layouts/application.html.erb and modifying the <body>..</body> section to look like the following:

  <body>
    <%# jchung, 4/2026, per https://guides.railsgirls.com/devise %>
    <% if notice %>
      <p class="alert alert-success"><%= notice %></p>
    <% end %>
    <% if alert %>
        <p class="alert alert-danger"><%= alert %></p>
    <% end %>

    <%# jchung, 4/2026, per https://guides.railsgirls.com/devise %>
    <p class="navbar-text float-right">
    <% if user_signed_in? %>
      Logged in as <strong><%= current_user.email %></strong>.
      <%= link_to "Edit profile", edit_user_registration_path, class: "navbar-link" %> |
      <%= link_to "Logout", destroy_user_session_path, data: { turbo_method: :delete }, class: "navbar-link"  %>
    <% else %>
      <%= link_to "Sign up", new_user_registration_path, class: "navbar-link"  %> |
      <%= link_to "Login", new_user_session_path, class: "navbar-link"  %>
    <% end %>
    </p>

    <%= yield %>
  </body>

Note: Adding the code to the application view (application.html.erb) ensures that the login status message and navbar links show up in every view of the app.

7. Force redirect to the login page if user has not logged in.

Edit app/controllers/application_controller.rb and add:

before_action :authenticate_user!

inside the class definition.

You may need to stop and restart the rails server and login to the mydiary app.