Skip to content

Instantly share code, notes, and snippets.

@partydrone
Created May 11, 2009 23:20
Show Gist options
  • Save partydrone/110233 to your computer and use it in GitHub Desktop.
Save partydrone/110233 to your computer and use it in GitHub Desktop.
<% error_handling_form_for @article do |f| -%>
<%= f.text_field :title %>
<%= f.date_select :post_date, :order => [:month, :day, :year] %>
<%= f.text_area :body %>
<%= f.submit 'Submit' %>
<% end -%>
class ArticleSweeper < ActionController::Caching::Sweeper
observe Article
def after_save(article)
expire_cache(article)
end
def after_destroy(article)
expire_cache(article)
end
def expire_cache(article)
expire_page(formatted_ticker_articles_path(:rss))
expire_page(article_path(article))
end
end
class ArticlesController < ApplicationController
before_filter { |c| c.send :set_page_title, ['News'] }
before_filter :login_required, :except => [:index, :ticker, :show]
caches_page :ticker, :show
cache_sweeper :article_sweeper, :only => [:create, :update, :destroy]
# GET /articles
def index
@articles = Article.current
end
# GET /articles/ticker
def ticker
@articles = Article.current(:limit => 5)
respond_to do |format|
format.rss # /articles/ticker.rss
end
end
# GET /articles/:id
def show
@article = Article.find(params[:id])
@page_title << @article.title
end
# GET /articles/new
def new
@article = Article.new
end
# GET /articles/:id/edit
def edit
@article = Article.find(params[:id])
end
# POST /articles
def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = "<em>#{@article.title}</em> was successfully created."
redirect_to articles_path
else
render :action => 'new'
end
end
# PUT /articles/:id
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = "<em>#{@article.title}</em> was successfully updated."
redirect_to articles_path
else
render :action => 'edit'
end
end
# DELETE /articles/:id
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
end
<h1 id="page_title">New Article</h1>
<%= render :partial => 'form' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment