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
The
Gemfile
in
mydiary
belongs to
bundler
, which manages Ruby gems dependencies for your new rails project.
Database (SQLite3) Config
Using "Scaffolding"
rails generate scaffold Entry title:string content:text
For our mydiary application, entries will initially solely consist of a title and some content.
There are other attributes (or “fields” in the database sense) that Rails will add by default to the underlying database table, such as id (a unique numeric identifier).
A directive is also added into the default migration (database creation) code to create timestamp columns: created_at (a timestamp of when the record/associated object was created), and updated_at (a timestamp of when the record/associated object was amended last).
Because of this automation, it is only necessary to specify the two custom, additional attributes to the scaffold generator to get things going.
Migrations to create db tables
We're building an online diary.
Our model is the diary entry that we'll call Entry.
The scaffolding step above defined our database table and its fields to reflect the structure of our model.
In Rails, models and database tables generally have a direct relationship.
# .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
# 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 %>