Project

General

Profile

Support #707

Updated by Daniel Curtis over 8 years ago

{{>toc}} 

 This is a guide on how I setup my Ruby on Rails development environment to create a new RoR web application on Arch Linux.  

 h1. h2. Prepare the Environment 

 * Make sure the system is up to date: 
 <pre> 
 sudo pacman -Syu 
 </pre> 

 * Install a few dependencies: 
 <pre> 
 sudo pacman -S ruby nodejs npm python2 
 </pre> 

 * Add bob to the sudo group: 
 <pre> 
 sudo usermod -aG sudo bob 
 </pre> 

 h1. h2. Install Single-user RVM 

 * Imports the RVM signing key: 
 <pre> 
 gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 
 </pre> 

 * Download and install rvm as the *bob* user: 
 <pre> 
 curl -L get.rvm.io | bash -s stable 
 </pre> 

 * Run the @.profile@ script at login: 
 <pre> 
 echo 'source ~/.profile' >> ~/.bash_profile 
 </pre> 

 * Then rerun the bash login script: 
 <pre> 
 source ~/.bash_profile 
 </pre> 

 * Add bob to the rvm group: 
 <pre> 
 sudo usermod -aG rvm bob 
 </pre> 
 #* *Logout* and then *login* again for the group changes to take effect. 

 * Check any missing requirements for RVM: 
 <pre> 
 rvm requirements 
 </pre> 

 h1. h2. Install Ruby 

 * List the known available ruby versions: 
 <pre> 
 rvm list known 
 </pre> 

 * Install Ruby 2.1: 
 <pre> 
 rvm install 2.1 
 </pre> 

 * Install the rails gem: 
 <pre> 
 gem install rails 
 </pre> 

 h1. h2. Create New Messaging Web App 

 NOTE: This section was adapted from the Ruby on Rails tutorial on "Codecademy":https://www.codecademy.com 

 * Create the new Ruby on Rails web application: 
 <pre> 
 rails new message_app new_app 
 </pre> 

 * Install the new web applications dependencies: 
 <pre> 
 bundle install 
 </pre> 

 * Start the rails development web server: 
 <pre> 
 rails s 
 </pre> 
 #* *NOTE*: The development web server bind the IP address to the localhost by default, so accessing it over the network will not work. To allow network access to the development web server: 
 <pre> 
 rails s - b 0.0.0.0 
 </pre> 

 h2. Beginning Development 

 h3. The Basics 

 * Create a controller for the Page class: 
 <pre> 
 rails generate controller Page 
 </pre> 

 * Edit the Page controller: 
 <pre> 
 vi app/controllers/page_controller.rb 
 </pre> 
 #* And create the home method: 
 <pre> 
 class PageController < ApplicationController  
   def home 
   end 
 end 
 </pre> 

 * Edit the routes file: 
 <pre> 
 vi config/routes.rb 
 </pre> 
 #* And add the route for the default landing page: 
 <pre> 
 Rails.application.routes.draw do 
   get '/' => "page#home" 
 end 
 </pre> 

 * Edit the home page view: 
 <pre> 
 vi app/views/page/home.html.erb 
 </pre> 
 #* And add the following bare html page: 
 <pre> 
 <div class="main"> 
   <div class="container"> 
     <h1>Hello World!</h1> 
   </div> 
 </div> 
 </pre> 

 h2. Create the Model 

 * Start by creating a Message model: 
 <pre> 
 rails generate model Message 
 </pre> 

 * Edit the database migrate file: 
 <pre> 
 vi db/migrate/20151209164020_create_messages.rb 
 </pre> 
 #* And add the @*t.text :content*@ to create the content row to the messages table: 
 <pre> 
 class CreateMessages < ActiveRecord::Migration 
   def change 
     create_table :messages do |t| 
       t.text :content 
       t.timestamps 
     end 
   end 
 end 
 </pre> 

 * Run the database migration: 
 <pre> 
 rake db:migrate 
 </pre> 

 h2. Create the Controller 

 * Create the Messages controller: 
 <pre> 
 rails generate controller Messages 
 </pre> 

 * Next edit the routes file: 
 <pre> 
 vi config/routes.rb 
 </pre> 
 #* Then map the URL /messages to the Messages controller index action: 
 <pre> 
 Rails.application.routes.draw do 
   get '/messages' => "messages#index" 
 end 
 </pre> 

 * Edit the Messages controller: 
 <pre> 
 vi app/controllers/messages_controller.rb 
 </pre> 
 #* And define an index method with a class array named @messages that contains all the messages in the database table: 
 <pre> 
 class MessagesController < ApplicationController 
   def index  
     @messages = Message.all  
   end 
 end 
 </pre> 

 h2. Create the View 

 * Edit the messages index Ruby HTML view: 
 <pre> 
 vi app/views/messages/index.html.erb 
 </pre> 
 #* Then create a for loop to loop through the contents of the @messages array: 
 <pre><code class="ruby"> 
 <div class="header"> 
   <div class="container"> 
     <h1>Messenger</h1> 
   </div> 
 </div> 

 <div class="messages"> 
   <div class="container"> 
     <% @messages.each do |message| %>  
     <div class="message">  
       <p class="content"><%= message.content %></p>  
       <p class="time"><%= message.created_at %></p>  
     </div>  
     <% end %> 
   </div> 
 </div> 
 </code></pre> 

 h1. Resources 

 * https://wiki.archlinux.org/index.php/Ruby_on_Rails 
 * https://wiki.archlinux.org/index.php/RVM#Multi-user_installation 
 * https://www.codecademy.com/en/courses/learn-rails

Back