Railsbridge Ruby on Rails

Railsbridge
(http://railsbridge.org/)

Learn to code or level up with RailsBridge!

RailsBridge workshops are a free and fun way to get started or level up with Rails, Ruby, and other web technologies. Our events focus on increasing diversity in tech, so that people of all backgrounds can feel welcome and comfortable in our industry.

We teach people to code because we believe that the people making technology should accurately reflect the diversity of those using it.

We want to push for all kinds of diversity in tech: gender, race, sexual orientation, ability, and class. Women were the first population we focused on, but aren’t the last.

We value acceptance, enthusiasm, and hard work. The RailsBridge community thrives on optimism and a love for making great things.

Setup
Installfest
(http://docs.railsbridge.org/installfest/)
Instructions for installing Ruby and Rails on your computer. You need to complete these steps before starting a Rails workshop! HIGHLY RECOMMENDED!

This is a set of step-by-step instructions for installing Ruby, Rails, and other important tools on Mac, Windows, or Linux computers. Originally developed for the Railsbridge Workshops, it is a community-supported open-source documentation project.


Rails
Intro To Rails
(http://docs.railsbridge.org/intro-to-rails/)

The “classic” RailsBridge curriculum (Suggestotron). Takes you step-by-step through making a Rails app, one command at a time, using helpers like rails generate scaffold, and deploying that app to the Internet.

To teach you Ruby on Rails we are going to use a “Real World” example. You’ve decided to create a voting system for you and your friends to play with. You’ve decided at a minimum, you’d like to allow users to:

  • view the topics sorted by number of votes
  • vote on topics
  • create, edit, and destroy topics

You’ve sketched up an initial screenshot of what you want it to look like:

Browser window with topic titles that can be voted on, ordered by number of votes


Glossary
(http://docs.railsbridge.org/installfest/glossary)

If you are ever stuck trying to make sense of all this alphabet soup – these glossaries may be helpful!

Have a look at the general Glossary, the Command Line Glossary, and the Ruby and Rails Glossary.


Commands
(http://docs.railsbridge.org/message-board/commands)

Ruby

Open an interactive Ruby terminal (type ‘exit’ to quit)

irb

Run a ruby program named FILENAME.rb

ruby FILENAME.rb

Installs a gem called GEMNAME

gem install GEMNAME

Installs gems listed in the Gemfile

bundle install

Rails

Create a new rails project called NAME

rails new NAME

Auto-generate routes (this can also be done manually)

rails generate scaffold

Create a new [Rails model]

rails generate model MODELNAME

Update the database to match what you have described in your code

rake db:migrate

Run the application locally (Ctrl-C to quit)

rails server

Start an interactive Ruby session that knows about your Rails models (type ‘exit’ to quit)

rails console

Print the routes for your application

rake routes

Browser

Go to the root page of your rails application

http://localhost:3000

Git

Creates a new git repository in your current directory.

git init

Add the current directory, and all sub directories, to your git repository.

git add .

Tells you what you’ve added, deleted, and changed between your current directory and you local git repository.

git status

Prints the difference between FILENAME and what is in your local git repository.

git diff FILENAME

Commit the files you’ve added to the local repository.

git commit -m "Describe what has changed, and why" .

Push committed changes to the remote server.

git push

Deploying to Heroku
First-Time Setup
(http://docs.railsbridge.org/intro-to-rails/deploying_to_heroku#every-time)

Every-Time
(http://docs.railsbridge.org/intro-to-rails/deploying_to_heroku#every-time)


Job Board
(http://docs.railsbridge.org/job-board/)
Build a simple job board from scratch with a little less of the magic of Rails scaffolding. This curriculum is great for a second or third RailsBridge attendee or for students who want to focus on how the app is wired together. (This curriculum doesn’t include deploying to the Internet.)

We’re going to build a job board in Rails using the tried-and-true method of following the errors that we make!

Rails generators will help us avoid some tedious file creation, but provide a lot less magic than scaffolding (which is what is used in the Suggestotron to create everything for a Topic all at once).

This means we’ll a get a little less done today than we did when we built Suggestotron. But we’re going to build an app in little pieces, so you can focus on understanding how the pieces fit together.

Contents


Message Board
(http://docs.railsbridge.org/message-board/)
Build a message board! This curriculum is for students who have completed the Suggestotron and the Job Board curricula. This curriculum is a challenge because it won’t tell you what to type in!

  • We’re going to build a message board system, where there are posts on the front page and you click through to see the original post plus discussion below.
  • We’ve divided this into challenges:
    • Challenge 1: Create a new rails app with a static home page
    • Challenge 2: Install Devise
    • Challenge 3: Make it pretty with Bootstrap
    • Challenge 4: Add pages to create and look at individual posts
    • Challenge 5: Make a posts index page
    • Challenge 6: Add replying
    • Challenge 7: Inline replying on a post
    • Challenge ∞: Other features of your choice
  • Each time you get your app into a functional state, before adding any more features, COMMIT TO GIT! The new features will probably break things, which is neat, but you’ll want to be able to roll back to a prior version if necessary.

Contents


Creating a New Controller

(http://docs.railsbridge.org/message-board/creating_a_new_controller)


MVC Overview
(http://docs.railsbridge.org/message-board/mvc_overview)

Explaining MVC and Records

MVC Overview

Rails implements a very specific notion of the Model/View/Controller pattern, which guides how you structure your web applications.

Model

  • saves data to the database
  • accesses data from the database
  • bridge between the database and objects

View

  • display the data for human (or machine) consumption
  • webpages are views

Controller

  • acts as the glue between the models and the views
  • combines data from multiple models
  • summarizes and filters data

In MVC, models, views, and controllers have very specific jobs. Separating responsibilities like this make it easy to maintain and extend rails applications. When responsibilities become muddied it gets much harder to debug issues and add new functionality.


The Request Cycle
(http://docs.railsbridge.org/message-board/the_request_cycle)

How does typing in a URL result in a web page being rendered? Here’s a rough overview.

  1. The user types in a URL, hoping for a cool website.
  2. After the DNS gets resolved (a topic for another day), the request hits a web server, which asks Rails what it’s got.
  3. Rails goes to the routes file first, which takes the URL and calls a corresponding controller action.
  4. The controller goes and gets whatever stuff it needs from the database using the relevant model.
  5. With the data the controller got from the model, it uses the view to make some HTML.
  6. Rails packages up the response and gives it to the web server.
  7. The web server delivers the response to the browser to display a cool website to the user.

 Ruby

Learn To Code
(http://docs.railsbridge.org/learn-to-code/)
A course which teaches how to code from the ground up, using Alex’s Learn To Code In Ruby curriculum. It’s geared towards people who may never have written code before and teaches just enough Ruby to get across basic principles like variables, objects, and the command line.

Contents

Ruby
(http://docs.railsbridge.org/ruby/ruby)
A ruby-specific curriculum, expanded from the “Ruby for Beginners” slide deck. Still new, with room for your contributions.

To teach you Ruby we are going to explain the basic building blocks used in the Ruby language and allow you reinforce what you’ve learned through challenges.

When you have completed this curriculum you should understand:

  • The command line and why we use it
  • How to run your Ruby code interactively (irb) or from a file
  • Simple types like numbers, strings, and booleans
  • Data structures like arrays and hashes
  • Object concepts like Classes

Contents


Methods vs. Functions
(http://docs.railsbridge.org/learn-to-code/methods)

  • a FUNCTION is a named chunk of code with PARAMETERS and a RETURN VALUE
  • a METHOD is a function that is attached to a specific object
    • it has privileged access to that object’s data
  • in Ruby everything’s an object, so the terms are mostly interchangeable

ToDo Learntocode
(http://docs.railsbridge.org/learn-to-code/todo-learntocode)

  • diagram: memory (variables+objects)
  • diagram: message passing
  • interpolation?
  • more on arrays
  • hashes
  • more labs (for students who “get it” before the rest of the room and don’t want to sit around bored (or help others))
  • functions
  • more array labs
  • methods
  • classes
  • File I/O
  • testing
  • symbols
  • rand