Skip to content

Instantly share code, notes, and snippets.

@naps62
Last active August 29, 2015 14:26
Show Gist options
  • Save naps62/0f92ed2a76932411f61e to your computer and use it in GitHub Desktop.
Save naps62/0f92ed2a76932411f61e to your computer and use it in GitHub Desktop.
cs2015-rails-presentation

Topics

  • MVC
  • Routes
  • Convention over Configuration
  • How to create a Rails project
  • Directory Structure
  • Route helpers
  • (*) sprockets (manifest files, minification, etc, Scss)
  • (*) views (view helpers

(*) These will probably be delayed to a different presentation

MVC

How the web works (recap)

  • (browser makes a request, server responds with a response, usually HTML). Repeat
  • From sinatra to Rails, this remains the same. The changes are only inside the server

What is MVC

  • Models, you already know them
  • Views, you already know them, but there are new things in Rails
  • Controllers: handle user requests, by interacting with models, and rendering views to generate the final HTML response

Routes

  • Routes go in config/routes.rb, and link to controller actions

get '/', to: 'test#my_action' links to TestController#my_action

Route convetions

In sinatra you did something like this:

* GET  /users            (index)
* GET  /users/:id        (show)
* POST /users            (create)
* POST /users/:id/edit   (edit)
* POST /users/:id/delete (destroy)

LIVE DEMO: sinatra-almost-rails app

  • Introduce PUT and DELETE

Introducing PUT and DELETE, you can shorten this to:

* GET    /users     (index)
* GET    /users/:id (show)
* PUT    /users/:id (update)
* DELETE /users/:id (destroy)

Now, we usually want the following pages:

  • a list of users
  • show a single user
  • show a form to create a new user (which goes to POST when submitting)
  • show a form to edit an existing user (which goes to PUT when submitting)
  • (delete action, which does not require a page)

How could we model this?

* GET    /users          (index)
* GET    /users/new      (new)
* POST   /users          (create)
* GET    /users/:id      (show)
* GET    /users/:id/edit (edit)
* PUT    /users/:id      (update)
* DELETE /users/:id      (destroy)

So how about we follow this convention all the time?

get  '/users',          to: 'users#index'
get  '/users/new',      to: 'users#new'
post '/users',          to: 'users#create'
get  '/users/:id',      to: 'users#show'
get  '/users/:id/edit', to: 'users#edit'
put  '/users/:id',      to: 'users#update'
delete '/users/:id',    to: 'users#destroy'

Which in rails translates to:

resources :users

Convention over Configuration

Some you have already seen from ActiveRecord:

  • User model relates to the users table
  • belongs_to :user refers to a user_id column, and links to an instance of the User model
  • has_many :users, is the plural form of :user, so it links to instances of the User model

Some other conventions:

  • UsersController should handle the User model
  • Resource actions are [:index, :show, :new, :create, :edit, :update, :destroy]
  • views are in app/views/controller_name/action_name.erb
  • main layout is in app/views/layouts/application.html.erb

How to create a Rails project

$ gem install rails
$ rails new my_project

Rails Structure

(explain this directories for a bit)

* app/
  * assets/
  * controllers/
  * models/
  * views/
* config/
  * initializers/
  * routes.rb
  * database.yml
* db/ # (same as before)
* lib/

Route helpers

You currently do this:

<a href="/posts/<%= post.id %>">Post Title</a>

But Rails routes make this easier:

<a href="<%= post_path(post) %>Post Title</a>"

Some other helpers:

get  '/users',          to: 'users#index'    => users_path
get  '/users/new',      to: 'users#new'      => new_user_path
post '/users',          to: 'users#create'   => users_path (and method: :post)
get  '/users/:id',      to: 'users#show'     => user_path(user)
get  '/users/:id/edit', to: 'users#edit'     => edit_user_path(user)
put  '/users/:id',      to: 'users#update'   => user_path(user) (and method: :put)
delete '/users/:id',    to: 'users#destroy'  => user_path(user) (and method: :delete)

or any other:

get '/something', to: 'my_controller#my_action', as: :custom

custom_path
#=> '/something'

Exercicio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment