Intro to Rails

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

What we will cover today

  • Homework Review
  • Controllers
  • Routes
  • Views

We will talk about Ratings, but focus on code for Artists and Songs only.

Homework Review

Rails Console

Did everyone create lots of objects?

Controllers & Views

Action View and Action Controller are the two major components of Action Pack. 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). Typically, Action Controller will be concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the response.

What is a controller?

After routing 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...

Pop Weasel 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
          

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...

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...

Pop Weasel 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 Artist Index and Show views.

Let's Develop It!

Build your own Song Show view

Let's Develop It!


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

From the album: <%= @song.album %>.

Where Does CSS Go?

app/assets/stylesheets

Summary

Today, we built the controllers and views for our application, including forms to create new songs, 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.