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 %>