Skip to content

Instantly share code, notes, and snippets.

@Lankenau
Lankenau / devise.md
Last active August 29, 2015 14:22 — forked from raghubetina/devise.md

Authentication and Authorization with Devise

We will be using the [Devise gem][2] to help us get started with authentication (are you who you say you are?) and authorization (are you allowed to do/see this?).

Add sign-in/sign-out

  • Add gem 'devise' to your Gemfile and bundle
  • rails g devise:install

Devise will give you some setup instructions. We don't need to worry about most of them, but we do need to set a root URL. Usually, you will point the root URL to the index action of some important resource in your application: In config/routes.rb:

@Lankenau
Lankenau / rcav.md
Last active August 29, 2015 14:21 — forked from raghubetina/rcav.md

RCAV Flowchart

Our apps are nothing more than a collection of URLs that we decide to allow users to access:

  • mydomain.com/products
  • mydomain.com/photos/193
  • mydomain.com/signin

So remember: everything always starts with a route between a URL we want to support and a Ruby method that will be responsible for generating a response to the user's browser.

CRUD with Ruby

Here is a quick reference on how to insert, retrieve, update, and delete rows from your database tables using our ActiveRecord-backed Ruby classes.

Adding Tables to the Database

First, we need a Ruby class to represent the real-world thing we're trying to model, and we need an underlying database table to store information about each individual thing.

Rails includes an easy generator to help us get set up with both of these things quickly. Supposed I wanted to create a table to store instructors, with two string columns first_name and last_name. I could use this shortcut from the Command Line:

Installing Ruby and Rails

Ruby is a wonderfully simple language, and Rails is an astonishingly powerful framework. Unfortunately, installing them can sometimes be the hardest part of learning to code.

In this guide I will lay out the least error-prone method of installing that we've found. Hopefully you will have no issues and it will be smooth sailing.

In case you do run into an error, first try restarting your computer and then re-try the last step that you got stuck on. If that doesn't work, we'll fix it together.

If you can't install Ruby on your laptop for some reason (for example, it belongs to your employer and you are not allowed to install things), skip to here.

Command Line Cheatsheet

See what folder you are currently in

Mac

The "print working directory" command:

pwd

Customizing Your Terminal Prompt

tl;dr

Run the following command from anywhere in Terminal:

echo PS1=\"\\w $ \" >> ~/.bash_profile
@Lankenau
Lankenau / rcav.md
Last active August 29, 2015 14:20 — forked from raghubetina/rcav.md

RCAV Flowchart

Our apps are nothing more than a collection of URLs that we decide to allow users to access:

  • mydomain.com/products
  • mydomain.com/photos/193
  • mydomain.com/signin

So remember: everything always starts with a route between a URL we want to support and a Ruby method that will be responsible for generating a response to the user's browser.

CRUD with Ruby

Here is a quick reference on how to insert, retrieve, update, and delete rows from your database tables using our ActiveRecord-backed Ruby classes.

Adding Tables to the Database

First, we need a Ruby class to represent the real-world thing we're trying to model, and we need an underlying database table to store information about each individual thing.

Rails includes an easy generator to help us get set up with both of these things quickly. Supposed I wanted to create a table to store instructors, with two string columns first_name and last_name. I could use this shortcut from the Command Line: