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
mydiary directory will make up the mydiary rails web app.Gemfile in mydiary belongs to bundler, which manages Ruby gems dependencies for your new rails project.Gemfile can be customized in a text editor if needed to add custom gem dependencies to your project.database.yml is a YAML file with info about the database(s) that the app will use.rails generate scaffold Entry title:string content:text
generate scaffold command. It will be a .rb script found in db/migrate that looks something like this:# .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
rake command to create the database tables using a migration.rake tasks are administrative tasks associated with your app that are managed by the rake tool.make command used for C/C++ development.Rakefile in the root of your app directory structure.rake command, you should notice a SQLite database file development.sqlite3 in the db project directory.development.sqlite3 will be found in the storage project directory. ### 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
config/environments/development.rb file and add the following code somewhere inside the Rails.application.configure do block:# Authorize access from any host: config.hosts.clear
rails server -b 0.0.0.0 -p <port> ### <port> = some value >= 3000 ###
rails server… command should start a simple development web server to serve your app.app/controllershttp://localhost:<your chosen port>/entries # e.g., http://localhost:3010/entries
app/controllers/entries_controller.rb, which contains a class definition for the Entries controller and its methods.ApplicationController and ActionController classes.index, new and create methods all make calls to methods of the Entry class, such as Entry.new and Entry.all.Entry is the model class, which inherits methods from the generic ActiveRecord (or ApplicationRecord) class that allow navigating and finding data in the database table for the Entry model.Entry.find(:all) returns all records as an array of objects from the entries database table.app/views/entries..erb files in app/views/entries are HTML template files containing Embedded Ruby.<% and %>_entry.html.erb and show.html.erb.render method.<%### _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>
app/controllers/entries_controller.rb):# 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
config/routes.rb file, and add some statements just BEFORE the following line (2nd line of file):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'
app/views/entries.app/views/entries/view_all.html.erb.view_all.html.erb, and then try using the view_all action again in the web browser:<%# 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
app/views/entries/show.html.erb).index view with the following ERB code:<%# 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 %>
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.