This tutorial guides college students in creating a simple Rails app for managing a list of names and phone numbers. The app will be developed on a remote Ubuntu Linux server using Visual Studio Code (VS Code) with GitHub Copilot.
1. Setting Up the Remote Ubuntu Linux Server
Step 1.1: Update and Install Required Packages
- Update the package list:
sudo apt update && sudo apt upgrade -y - Install dependencies:
sudo apt install -y curl git build-essential libssl-dev libreadline-dev zlib1g-devsudo apt install -y libbz2-dev libsqlite3-dev libffi-dev
sudo apt install -y libyaml-dev
Step 1.2: Install Ruby and Rails
- Install
rbenvto manage Ruby versions:git clone https://github.com/rbenv/rbenv.git ~/.rbenvecho 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init - bash)"' >> ~/.bashrcexec $SHELL - Install
ruby-buildfor managing Ruby installations:git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build - Install the latest stable version of Ruby:
rbenv install 3.2.2rbenv global 3.2.2 - Install Rails:
gem install railsrbenv rehash
Step 1.3: Install PostgreSQL (Optional)
For better database management, install PostgreSQL:
sudo apt install -y postgresql postgresql-contrib libpq-dev sudo systemctl enable postgresql sudo systemctl start postgresql sudo -u postgres createuser -s phoneuser
Step 1.4: Create the Rails App
- Generate a new Rails app:
rails new contacts_app --database=postgresqlcd contacts_app - Set up the database:
- Edit
config/database.ymlto configure the PostgreSQL username and password. - Initialize the database:
rails db:create
- Edit
2. Setting Up Visual Studio Code for Remote Development
Step 2.1: Install VS Code and Extensions
- Download and install VS Code from https://code.visualstudio.com/.
- Install the following extensions:
- Remote – SSH: To develop on the remote server.
- Ruby: For Ruby syntax highlighting and IntelliSense.
- GitHub Copilot: For AI-assisted coding.
Step 2.2: Configure Remote-SSH
- Open the command palette in VS Code (
Ctrl+Shift+PorCmd+Shift+P) and selectRemote-SSH: Add New SSH Host. - Enter the SSH command for your server:
ssh username@your-server-ip - Save the configuration and connect to the server using
Remote-SSH: Connect to Host. - Navigate to the
contacts_appdirectory in the VS Code file explorer.
3. Enabling GitHub Copilot
Step 3.1: Install GitHub Copilot
- Install the GitHub Copilot extension in VS Code.
- Log in to your GitHub account when prompted.
Step 3.2: Use GitHub Copilot
- As you type code, GitHub Copilot suggests completions. Press
Tabto accept a suggestion.
4. Building the Rails App
Step 4.1: Generate the Model
Use GitHub Copilot to simplify model creation:
- Run the following command to generate the
Contactmodel: rails generate model Contact name:string phone:string- Run migrations to apply changes to the database:
rails db:migrate - Open
app/models/contact.rband use Copilot to add validations. For example: class Contact < ApplicationRecord validates :name, presence: true validates :phone, presence: true, length: { is: 10 } end
Step 4.2: Create the Controller and Views
- Generate the controller:
rails generate controller Contacts- Open
app/controllers/contacts_controller.rband use Copilot to implement theindex,new,create,edit,update, anddestroyactions. Example forindex:def index @contacts = Contact.all end - Create views for
index,new, andeditactions. For example, createapp/views/contacts/index.html.erb:<h1>Contacts</h1> <table> <tr> <th>Name</th> <th>Phone</th> </tr> <% @contacts.each do |contact| %> <tr> <td><%= contact.name %></td> <td><%= contact.phone %></td> </tr> <% end %> </table>
Step 4.3: Add Routes
- Open
config/routes.rband define resources:Rails.application.routes.draw do resources :contacts root "contacts#index" end
Step 4.4: Test the App
- Start the Rails server:
rails server -b 0.0.0.0 - Open your app in a browser at
http://your-server-ip:3000. - Add, edit, and view contacts.
5. Using GitHub Copilot for Efficiency
Example 1: Auto-generating Validations
- Type
validatesin theContactmodel, and Copilot will suggest validations for attributes.
Example 2: Writing Controller Actions
- Type
def createincontacts_controller.rb, and Copilot will suggest the entirecreateaction with strong parameters.
Example 3: Creating Forms
- Start typing a form in
new.html.erb, and Copilot will auto-complete aform_withblock.
6. Deploying the App (Optional)
Step 6.1: Install Passenger and Nginx
- Install Passenger and Nginx:
sudo apt install -y libnginx-mod-http-passenger - Configure Nginx to serve the Rails app.
Step 6.2: Deploy the App
- Precompile assets:
rails assets:precompile - Restart Nginx:
sudo systemctl restart nginx - Access your app at
http://your-server-ip.
