idflores / rails-tutorial

my personal repository for learning Ruby on Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rails Tutorial

My personal repository for learning Ruby on Rails


Note

Rails and Django share many concepts. For more conceptual notation, visit my Django-Tutorial repository.

Discussion

The philosophy between Rails and Django are incredibly similar. From my research, the differences that spawn Rails development over Django are mostly driven by opinion specifically when concerning application routing and Rails' "Convention over Configuration". This is really the key philosophical difference, here, that drives the majority of programming differences between it and Django. By "convention", Rails includes tools such as Coffeescript, Sass, and the jQuery Unobtrusive Adapter out of the box. However, Django's emphasis on user configuration seems to allow for more freedom for seasoned developers.


Ruby on Rails Tutorial

Updating Ruby

Get Homebrew

from: https://brew.sh

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Remove RVM to install rbenv (optional)

(RECOMMENDED)

Note: in order to install rbenv, you may need to uninstall RVM. This StackOverflow Response proved to be a very useful guide to doing so...

Note: in order to completely remove RVM, I had to modify my $PATH by:

  1. perform echo $PATH in your terminal
  2. copy the output and remove all directories including .../.rvm/... in a text editor
  3. copy the new $PATH
  4. perform command in terminal
export PATH=<paste_new_$PATH>
  1. restart Terminal (or iTerm)
  2. check gem is pointing to the right directory using:
gem env home
# /Users/<username>/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0

Install Ruby with rbenv

from: https://gorails.com

brew install rbenv ruby-build

# Add rbenv to bash so that it loads every time you open terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile

# reload the ~/.bash_profile file
source ~/.bash_profile

# Update Ruby
rbenv install 2.4.1
rbenv global 2.4.1

# Verify Ruby was installed
ruby -v

Install Rails

gem install rails

Create A New Rails Application

In the project directory, run:

rails new <app_name>

Run the Rails server

cd <app_name>
bin/rails server

Using your favorite browser (please! no IE), type localhost:3000

Note: the development server reloads your files on every save while in the development environment


Controller

Highlights

  • handles requests for the given application
    • routing determines which controller receives which requests
    • action collects and serves information to views
  • collects information
  • it is convention to implement CRUD actions in this order:
    • index, show, new, edit, create, update, destroy

Generate Controllers

Within the Rails application directory:

bin/rails generate controller <controller_name> <action_name>

Note: also generates an action called <action_name> (optional)


View & Templates

Highlights

  • handles how information is displayed
  • uses eRuby (Embedded Ruby) to write view templates
    • uses the .erb file extension
  • supports partial files to prevent duplication in templates
    • by convention, partial file names begin with an _ (underscore)
      • Example: .../_<fileName>.html.erb
    • heavily uses Resource-Oriented Style

Model

Highlights

  • manages databases for the application
    • concept includes both models/ and db/ directories
  • migrations ruby classes designed to create and modify database tables
    • Rails uses rake commands to run migrations
    • naming convention includes a timestamp
      • Example: db/migrate/YYYYMMDDHHMMSS_<action_name>_<model_name>.rb
    • actions defined in Rails generated migration are reversible
  • includes data validation methods documentation
  • supports Active Record Association

Generate Models

Within the Rails application directory:

bin/rails generate model <model_name> <attribute>:<type>

Note: also generates an attribute called of type (optional)

Run migrations

Within the Rails application directory:

bin/rails db:migrate

Note: command only functional in the development environment

Authentication

Highlights


Development Environment

Tools

Resources

About

my personal repository for learning Ruby on Rails

License:MIT License


Languages

Language:Ruby 72.9%Language:HTML 19.7%Language:CSS 3.1%Language:JavaScript 2.9%Language:CoffeeScript 1.6%