Action Pack: HTTP Loving
如你所期望的那样,Rails2.0的Action Pack更加贴近HTTP,并且充分利用HTTP协议,例如资源、多种视图,还有更多的呢。我们添加了一个模块来处理HTTP的Basic验证,它能够让授权的API轻松跨越SSL协议,而且他是如此的简单易用。下面是一个例子(更多的例子请参考 ActionController::HttpAuthentication):
Ruby代码 复制代码
class PostsController < ApplicationController
USER_NAME, PASSWORD = "dhh", "secret"
before_filter :authenticate, :except => [ :index ]
def index
render :text => "Everyone can see me!"
end
def edit
render :text => "I'm only accessible if you know the password"
end
private
def authenticate
authenticate_or_request_with_http_basic do |user_name, password|
user_name == USER_NAME && password == PASSWORD
end
end
end
如你所期望的那样,Rails2.0的Action Pack更加贴近HTTP,并且充分利用HTTP协议,例如资源、多种视图,还有更多的呢。我们添加了一个模块来处理HTTP的Basic验证,它能够让授权的API轻松跨越SSL协议,而且他是如此的简单易用。下面是一个例子(更多的例子请参考 ActionController::HttpAuthentication):
Ruby代码 复制代码
class PostsController < ApplicationController
USER_NAME, PASSWORD = "dhh", "secret"
before_filter :authenticate, :except => [ :index ]
def index
render :text => "Everyone can see me!"
end
def edit
render :text => "I'm only accessible if you know the password"
end
private
def authenticate
authenticate_or_request_with_http_basic do |user_name, password|
user_name == USER_NAME && password == PASSWORD
end
end
end