16. con g.ru
# This file is used by Rack-based
# servers to start the application.
require ::File.expand_path(
'../config/environment',
__FILE__
)
run Tutorial::Application
17. con g/boot.rb
require 'rubygems'
# Set up gems listed in the Gemfile.
gemfile = File.expand_path(
'../../Gemfile',
__FILE__
)
if File.exist?(gemfile)
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
end
19. con g/environment.rb
# Load the rails application
require File.expand_path(
'../application',
__FILE__
)
# Initialize the rails application
Tutorial::Application.initialize!
20. con g/application.rb (1)
require File.expand_path(
'../boot',
__FILE__
)
require 'rails/all'
if defined?(Bundler)
Bundler.require(:default, Rails.env)
end
21. con g/application.rb (2)
module Tutorial
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters +=
[:password]
end
end
22. environments/production.rb
Tutorial::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching =
true
config.action_dispatch.x_sendfile_header =
"X-Sendfile"
config.serve_static_assets = false
end
24. script/rails (1)
#!/usr/bin/env ruby
# This command will automatically
# be run when you run "rails" with
# Rails 3 gems installed from the
# root of your application.
ENV_PATH = File.expand_path(
'../../config/environment',
__FILE__
)
67. Default Resource Route
controller :posts do
scope "/posts" do
get "/" => :index
post "/" => :create
get "/:id" => :show
put "/:id" => :update
delete "/:id" => :delete
get "/new" => :new
get "/:id/edit" => :edit
end
end
68. Default Resource Route
controller :posts do
scope "/posts" do
get "/" => :index, :as => :posts
post "/" => :create
get "/:id" => :show, :as => :post
put "/:id" => :update
delete "/:id" => :delete
get "/new" => :new, :as => :new_post
get "/:id/edit" => :edit, :as => :edit_post
end
end
103. Controller
def index
@posts = Post.
where("created_at < ?", Time.now).
order("publish_date desc")
end
104. Model
scope :desc,
order("publish_date desc")
def self.past
where("created_at < ?",
Time.now).desc
end
105. Model
scope :desc,
order("publish_date desc")
def self.past
where("created_at < ?",
Time.now).desc
end
def self.recent(number)
past.limit(5)
end
106. Pagination
class PostsController
def index
@posts = Posts.page(5, :per_page => 10)
end
end
class Post < ActiveRecord::Base
def self.page(number, options)
per_page = options[:per_page]
offset(per_page * (number - 1)).
limit(per_page)
end
end
115. Be More Speci c
def welcome(user)
@user = user
mail(:to => user.email,
:subject => "Welcome man!") do |format|
format.html
format.text { render "generic" }
end
end
116. Defaults
default :from => "wycats@gmail.com"
def welcome(user)
@user = user
mail(:to => user.email,
:subject => "Welcome man!") do |format|
format.html
format.text { render "generic" }
end
end
118. Interceptors
class MyInterceptor
def self.delivering_email(mail)
original = mail.to
mail.to = "wycats@gmail.com"
mail.subject =
"#{original}: #{mail.subject}"
end
end
119. Interceptors
class MyInterceptor
def self.delivering_email(mail)
original = mail.to
mail.to = "wycats@gmail.com"
mail.subject =
"#{original}: #{mail.subject}"
end
end
config.action_mailer.
register_interceptor(MyInterceptor)