Skip to content

Instantly share code, notes, and snippets.

@bradly
Created September 23, 2009 06:56
Show Gist options
  • Save bradly/191798 to your computer and use it in GitHub Desktop.
Save bradly/191798 to your computer and use it in GitHub Desktop.
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
include Clearance::Authentication
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
def render_optional_error_file(status_code)
super unless render_error(status_code)
end
def render_error(error_type)
case error_type
when :not_found
@message = 'Page Not Found.'
@error_code = 404
when :internal_server_error
@message = 'Something has gone wrong.'
@error_code = 500
else
return false
end
respond_to do |type|
type.html { render :template => "errors/error", :layout => 'application', :status => error_type }
type.all { render :nothing => true, :status => error_type }
end
true
end
end
!!!
%html
%head
%title= "Postal Proxy"
= stylesheet_link_tag 'application'
= javascript_include_tag :all
%body(id=controller_name class=action_name)
= render :partial => 'layouts/flash_messages' if flash.present?
= render :partial => 'layouts/header'
#content
.frame= yield
= render :partial => 'layouts/footer'
%script(language="javascript" type="text/javascript" src="http://quickribbon.com/ribbon/2009/08/a798930f47f6eee0967cd49380dea890.js")
%h1.page_title Send a Postcard
-form_for @postcard do |f|
%fieldset.message_box
=f.label :body, '3. Your Message'
=f.text_area :body, :tabindex => '3'
%fieldset.address_box
=f.label :to_address, '1. To Address'
=f.text_area :to_address, :tabindex => '1'
%fieldset.address_box
=f.label :from_address, '2. From Address'
=f.text_area :from_address, :tabindex => '2'
=link_to_function 'Save', "$(this).up('form').submit()", :class => 'button'
=link_to 'Back', postcards_path, :class => 'button'
=link_to('Delete', [@postcard], :method => 'delete', :confirm => 'Are you sure?', :class => 'button') unless @postcard.new_record?
.clear
:javascript
Event.observe(window, 'load', function() {
$("postcard_to_address").focus();
});
class PostcardsController < ApplicationController
before_filter :authenticate
before_filter :find_postcard, :only => [:show, :edit, :update, :destroy, :deliver]
before_filter :ensure_unsent, :only => [:show, :edit, :deliver, :update]
def index
@unsent_postcards = current_user.postcards.unsent
@sent_postcards = current_user.postcards.sent
end
def show
render :action => :edit
end
def new
@postcard = current_user.postcards.new
end
def edit
end
def create
if @postcard = current_user.postcards.create(params[:postcard])
flash[:notice] = "Your postcard has been saved"
redirect_to postcards_path
else
flash[:error] = "There was an error saving your postcard"
render :action => 'new'
end
end
def update
if @postcard.update_attributes(params[:postcard])
flash[:notice] = "Your postcard has been saved"
redirect_to postcards_path
else
flash[:error] = "There was an error saving your postcard"
render :action => 'edit'
end
end
def deliver
if @postcard.deliver!
flash[:notice] = "Your postcard has been delivered"
else
flash[:error] = "There was an error delivering your postcard"
end
redirect_to postcards_path
end
def destroy
if @postcard.destroy
flash[:notice] = "Your postcard was successfully deleted"
redirect_to postcards_path
else
flash[:error] = "Your postcard was not successfully deleted"
redirect :back
end
end
protected
def find_postcard
@postcard = current_user.postcards.find(params[:id])
end
def ensure_unsent
if @postcard.sent_at.present?
flash[:error] = "This message has already been sent."
redirect_to postcards_path and return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment