peepcode-rails2 读书笔记...

class Recipe < ActiveRecord::Base
  validates_numericality_of :isdn,:greater_than=>5000,:allow_blank => true
  belongs_to :category
end
填表单时,首次创建不限制,但是修改的时候会要求isdn有一个数值范围

 4;35;1mCategory Load (0.016000)   SELECT * FROM categories WHERE (categories."id" = 1)
 4;36;1mCategory Load (0.000000)   SELECT * FROM categories WHERE (categories."id" = 2)
 4;35;1mCACHE (0.000000)   SELECT * FROM categories WHERE (categories."id" = 1)
在单个请求内,会以select语句为key,自动cache相同的select语句(这个特性,好像不是那么有用)

class CreatePosts < ActiveRecord::Migration
  create_table :posts do |t|
    t.integer :user_id, :category_id, :null => false
    t.text    :body
    t.timestamps
  end
  def self.down
    drop_table :posts
  end
end
创建表简化了不少,null应该默认不出现,否则老是异常...

stylesheet_link_tag "application", "forms", :cache => true
据说只要加了cache就自动合并到一个all文件里面...这个特性不错,再下一步就是ZIP压缩了

<% for post in @posts do %>
  <%= render :partial => 'article',
             :layout => 'article_simple',
             :locals => {:article => post} %>
<% end %>
partial是可以反复渲染的,现在一批partial能套在一个局部的layout上面了...

map.resources :categories do |t|
  t.resources :recipes
end
对应categories/2/recipes 这类的url,restful风格

class PostsController < ApplicationController
  rescue_from(ActiveRecord::RecordNotFound) do |exception|
    render :file => '/bad_record'
  end
  rescue_from NoMethodError, :with => proc do |e|
    render :text => e.message
  end
end
更简单的404页面或者错误处理...

class AdminController < ApplicationController
  before_filter :authenticate
  def authenticate   
    return false if (session[:login_tries].to_i > 3)
    if(@user = authenticate_with_http_basic { |name, pass|
                 User.authenticate(name, pass)
               })
      session[:login_tries] = nil
      return true
    else
      session[:login_tries] = session[:login_tries].to_i + 1
      request_http_basic_authentication
    end
  end
end
基本的http用户验证,管理员后台登录...

helper :all 默认加载所有的helper...

config/initializers 自动加载默认启动配置...

rake routes列表当前的url路径

db:create db:reset db:rollback db:migrate

@author = Author.find_by_name 'jim'
@articles = Post.paginate_by_author_id(@author.id,:page => params[:page])
新的分页的方法...

yaml_contents = File.open("#{RAILS_ROOT}/config/mailer.yml")
mailer_options = YAML.load(yaml_contents)
ActionMailer::Base.smtp_settings = mailer_options
在environment里面这么写,然后添加邮件配置信息到mailer.yml~

:address: "smtp.example.com"
:port: 587
:domain: "smtp.example.com"
:user_name: "[email protected]"
:password: "not-the-real-password"
:authentication: :plain

接下来竟然是几十页的svn上的changelog...囧


--------------
chenjinlai
2008-04-25

你可能感兴趣的:(SVN,cache,读书,Rails,ActiveRecord)