Girl Develop It Logo

Intro to Rails

slides: http://cherimarie.github.io/gdi-rails

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

What we will cover today

  • Homework Review
  • Controllers
  • Routes
  • Views

Homework Review

Rails Console
  1. Rails for Zombies
    • Problems?
  2. Rails Console Experimentation
    • Questions?

Controllers & Views

Action View and Action Controller get requests and respond with HTML.

In Rails, web requests are handled by Action Pack, which splits the work into a controller part (performing the logic) and a view part (rendering a template).

Action Controller is concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the HTML response.

Simple HTTP Request

simple HTTP diagram

HTTP Model-View-Contoller Request

simple REST MVC diagram

Rails Request

Rails request diagram

Detailed Rails Request

Detailed Rails request diagram

What is a controller?

After the routes.rb has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output (action and view).

More info...

            
    class ArtistsController < ApplicationController
      # route get 'songs#index' would direct here
      def index
        @artists = Artist.all
      end
      # route 'songs#show' would direct here
      def show
        @artist = Artist.find(params[:id])
        @songs = @artist.songs
      end
    end
            
          

BestSong Controllers


Controller Actions
Artists Index Show
Songs New Create Show
Ratings New Create

Building Controllers

Let's work together to build the Artists, Songs, and Ratings controllers. Start with running the generate commands in the terminal:


$ rails generate controller Artists
$ rails generate controller Songs
$ rails generate controller Ratings
          

Let's Develop It!

Build your own Song#new controller method. It will need to create a new Song and assign it to an instance variable, then find the artist by params, and assign them to instance variables.

Let's Develop It!


# in app/controllers/ratings_controller.rb

def new
  @song = Song.new
  @artist = Artist.find(params[:artist_id])
end
          

Routing

The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.

Since ratings belong to songs, and songs belong to artists, we need nested routes.

More info...

            
  # view all routes http:///rails/info/routes
  # in app/config/routes.rb
  resources :artists do
    resources :songs
  end
            
          

Routing View Page

http://<app-name-here>/rails/info/routes

rails routes view

What is a View

Action View templates are written using embedded Ruby in tags mingled with HTML. To avoid cluttering the templates with boilerplate code, a number of helper classes provide common behavior for forms, dates, and strings. It's also easy to add new helpers to your application as it evolves.

  • Layouts
  • Templates
  • Partials

More info...

BestSong Views

  • Model/View
  • Layouts/Application
  • Artist/Index (root)
  • Artist/Show
  • Song/Show
  • Song/New
  • Song/_form
  • Rating/New
  • Rating/_form

Building Views

Let's work together to build the Artists, Songs, and Ratings views.

Let's Develop It!

Build your own Song/Show view

Let's Develop It!


<h1><%= @song.title %></h1>

Best played at <%= @song.optimal_volume %>. Rated as <%= @song.average_rating %> by people who would know.

Where Does CSS Go?

app/assets/stylesheets

Summary

Today, we built the controllers and views for our application, including forms to create new songs and ratings, as well as all the necessary routing.

App code available on Github.

Questions

?

Homework

Ensure that you have an account set up on Heroku.com.

Read the documentation for the Devise gem. We'll be using it next week.