gem 'draper', '~> 1.0'
# app/models/article.rb
class Article < ActiveRecord::Base
attr_accessible :publication_status
end
# app/controllers/articles_controller.rb
def show
@article = Article.find(params[:id]).decorate #----
end
# app/decorators/article_decorator.rb
class ArticleDecorator < Draper::Decorator
delegate_all
def publication_status
if published?
"Published at #{published_at}"
else
"Unpublished"
end
end
def published_at
source.published_at.strftime("%A, %B %e")
end
end
就可以这样用了
<%= @article.publication_status %>