content_controller.rb

来自「ruby on rails web敏捷开发之路第二版 源代码」· RB 代码 · 共 92 行

RB
92
字号
#---# Excerpted from "Agile Web Development with Rails, 2nd Ed."# We make no guarantees that this code is fit for any purpose. # Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.#---class Article  PUBLIC = []  PREMIUM = []  def self.list_public    PUBLIC  end  def self.list_premium    PREMIUM  end  def initialize(arg)    @when = Time.now  end  def save    PUBLIC << self  end  def find(a)  endendclass User  def active?    true  end  def find(a)    self.new  endendclass ContentController < ApplicationController  before_filter :verify_premium_user, :except => :public_content  caches_page   :public_content  caches_action :premium_content  def public_content    @articles = Article.list_public  end  def premium_content    @articles = Article.list_premium  end  def create_article    article = Article.new(params[:article])    if article.save      expire_page   :action => "public_content"    else      # ...    end  end  def update_article    article = Article.new(params[:article])    if article.save      expire_action :action => "premium_content", :id => article    else      # ...    end  end  def delete_article    Article.destroy(params[:id])    expire_page   :action => "public_content"    expire_action :action => "premium_content", :id => params[:id]  end  private  def verify_premium_user    user = session[:user_id]    user = User.find(user) if user    unless user && user.active?      redirect_to :controller => "login", :action => "signup_new"    end  endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?