Rails Console Commands Using Mac Terminal
Posted on August 12, 2015 in Rails by Matt Jennings

Inside a Directory in Mac Terminal, Create a Rails Project named sample_app

rails new sample_app

OR without adding a unit test:
rails new sample_app --skip-test-unit
rails new sample_app -T

Go Inside the Rails Console

rails console
rails c

Start a New Rails Web Server

rails server
rails s

Install Gems for a Rails Project

  1. After running a command likeĀ rails new sample_app, open your sample_app directory in a code editor like RubyMine.
  2. Open the Gemfile file which will be in the project root directory (sample_app).
  3. Uncomment out the commented gem 'bcyrpt' line to enable the bcrypt Gem which will be used for creating encrypted passwords.
  4. BEFORE the group :development, :test do install the hirb Gem by adding the code below:
    gem 'hirb'
  5. Then to install this Gem for my Rails project, in the project directory in Terminal do:
    bundle install
  6. The bundle install command needs to be run after a the Gemfile file is edited and you add in new Gem, like gem 'hirb' that we entered above.

In Terminal, Create a Model called User that has first_name, last_name, email, and age Attributes

rails g model User first_name:string last_name:string email:string password:string age:integer

After that, create the users table by doing:
rake db:migrate

View an Empty User Object Instance in the Rails Console

Add a New User into the users Table in the Rails Console

User.create(first_name: "Jon", last_name: "Snow", age: 17, email: "", password: "password")

After Installing the hirb Gem in your Gemfile File, Use it the Rails Console to Rows in the users Table in an Easy Way using the Rails Console


If the Jon Snow user I entered into the users table is the Last Table Entry, Update It

User.last.update(age: 18)

After Installing the RSpec Gem in the Gemfile File, Create RSpec Files

rails g rspec:install

In a Rails App, How to View the Seven RESTful Routes

In the /config/routes.rb file, after the Rails.application.routes.draw do add the code below:
resources :products

Then in Terminal do:
rake routes

View Error Messages in the Rails Console

  • In a Rails App, setting up errors validation in a model file. Below is example code for a demo project directory and product.rb model file (/demo/app/models/product.rb) that only tests if the form fields for the name and description attributes in the products table are NOT blank:
    class Product < ActiveRecord::Base
      validates :name, :description, presence: true
  • Open Terminal and go into the rails console to do the steps below, pressing Enter after each line:
    rails c
    product = "", description: "")
  • Then, you’ll see the error message output in the Terminal below:
    ["Name can't be blank", "Description can't be blank"]

View the Rails RESTful Routes

  • If working inside a demo2 project directory, open the routes file listed below:
  • In the routes.rb file, under the line similar to Rails.application.routes.draw do add a line like below, with :users meaning that a users table has already been created:
    resources :users
  • To make the /users and / paths both point to the Users controller, index method add this code, also in routes.rb:
    root 'users#index'
  • Go back into your project directory in Terminal, and to view the Rails RESTful Routes do:
    rake routes

Create a Users Controller with the 5 Rails RESTful Routes GET Requests that Generates the Controller, View, RSpec, and Other Useful File(s)

NOTE: The create, update and destroy controller methods will be post, patch and delete requests respectively in routes.rb.

rails g controller Users index new show edit

Leave a Reply

To Top ↑