Live Notes For Refresh Ruby On Rails Workshop

November 20, 2010

> rails new blogpress

creates new directory contains:

– Gemfile – home to GEM dependencies
– app – where your models, views, and controllers
– config – this is where your configurations; routes, etc.
– db – database schema and migrations (SQLLite)
– log – development, test, and production logs
– public – web accessible directory, image, stylesheets, javascript, etc.
– Vendor – centralized location for 3rd party code from plugins to gem

> bundle install

bundler is a tool that installs required gems for your application. Bundler is able to determine dependencies and children without instructions from you

database configuration (database.yml)
defaults to sqlite, allows for mysql and postgreSQL

> rake db:create
– reads the datbase.yml file and creates db

rake, WTF?
Rake is a way to run tasks

rake -T = shows all tasks you can do

> rails server

> rm public/index.html

We don’t want this page displaying when visitors come to our blog

> rails generate controller home index

Generate a controller with only an index action to take the pklace of public/index.html

Update config/routes.rb to let the app know that our root should be
REMOVE get “home/index”
ADD root :to => “home#index”

Any rails file ends in .rb
Any rails view file is .erb


> rails generate scaffold Post author:string title:string content:text permalink:text

This file does ALL the work for you.
strings have a default length of 256 character
text has a default length of 1024 character (?)
float, decemial also other types

Scaffold creates full CRUD functionality for us, including our model, view, controller, and even our database mirgration

> rake db:migrate
Calls our self.up method to create the posts database table
Should be seeing “Listing posts”

> rails console

You can test pretty much anything right here.

(Use capital ‘P’ for the Posts object, not a particular post)

– You can add a .xml to the end to automatically spit out xml (rails by default spits out HTML)

– In your posts_controller.rb, add this for example to spit out JSON:
format.json { render :json => @posts }

Blog Comments:
> rails generate scaffold Comment post:references email:String content:text

> rake db:migrate


resources; comments
resources: posts


resources :posts do
resources :comments

/posts – /comments
/posts/1 – /comments/1
/posts/1/edit – /comments/1/edit

Changes now made urls like this:


@post = Post.find(params[:post_id])
@comments = @post.comments

We are going to use a Gem here
Add this to your Gemfile, at the end of your file (in your root):
gem ‘acts-as-taggable-on’

> bundle install

(might have to restart rails console before typing in the below:)
> rails generate acts_as_taggable_on:migration

> rake db:migrate


Don’t forget to update your show.html.erb to include:

<%= @post.tags %>

BLOG: Validation Options

:presence => true
:uniqueness => true
:numbericality => true
:length => { :minimum => 0, maximum => 2000 }
:format => { :with => /.*/ }
:inclusion => { :in => [1,2,3] }
:exclusion => { :in => [1,2,3] }
:acceptance = > true (deals with checkboxes)
:confirmation => true (deals with checkboxes)

validates :author, :presence => true, :length => {:maximum => 30 }
validates :title, :presence => true, :length => {:maximum => 100 }
validates :content, :presence => true

validates :email, :presence => true
validates :content, :presence => true

Blog: Validation

Basic HTTP authentications prompts users via a browser popup. This authentication lasts until the browser is closed.

Blog: Most Recent Posts

We want to display the top n posts on the main page, of course we’ll want the n more recent posts