Custom 404 Rails error pages
Ruby posted about 1 year ago by christian
All credit for this snippet is due to Henrik Nyh.
Put this in application_controller.rb:
1 alias_method :rescue_action_locally, :rescue_action_in_public if RAILS_ENV == 'development' 2 3 def render_optional_error_file(status_code) 4 if status_code == :not_found 5 activate_authlogic 6 render_404 7 else 8 super 9 end 10 end 11 12 def render_404 13 respond_to do |type| 14 type.html { render :template => "errors/error_404", :layout => 'application', :status => 404 } 15 type.all { render :nothing => true, :status => 404 } 16 end 17 18 true # so we can do "render_404 and return" 19 end
How to support conditional gets in Rails
Ruby posted about 1 year ago by christian
From the When to Tell Your Kids About Client Caching RailsConf presentation
1 def fresh?(response) 2 def stale?(:etag => @object, :last_modified => updated_at.utc) 3 def not_modified?(modified_at) 4 def etag_matches?(etag)
1 class PeopleController < ApplicationController 2 def show 3 @person = Person.find(params[:id]) 4 response.last_modified = @person.updated_at.utc 5 response.etag = @person 6 return head(:not_modified) if request.fresh?(response) 7 8 respond_to do |wants| 9 #... 10 end 11 end 12 end
1 response.etag = @person # => “5cb44721b6ce18857ff6900486dc4aba” 2 @person.cache_key # => "people/5-20071224150000"
1 class PeopleController < ApplicationController 2 def show 3 @person = Person.find(params[:id]) 4 if stale?(:etag => @person, :last_modified => @person.updated_at.utc) 5 respond_to do |wants| 6 #... 7 end 8 end 9 end 10 end
Bash scripting: change current Rails application directory
Shell Script (Bash) posted about 1 year ago by christian
Put this in ~/.bashrc:
1 app () { cd "/var/www/$*/current"; }
Execute:
1 . ~/.bashrc
Now you can change to another Rails app directory like this:
1 app xxx.com
Rails plugin template
Ruby posted about 1 year ago by christian
1 script/plugin generate PluginTemplate
1 module PluginTemplate 2 def self.included(base) 3 base.class_eval do 4 include InstanceMethods 5 extend ClassMethods 6 7 alias_method_chain :rescue_action_in_public, :gulag 8 alias_method_chain :rescue_action_locally, :gulag 9 end 10 end 11 12 module ClassMethods 13 end 14 15 module InstanceMethods 16 end 17 end
init.rb:
1 ActionController.send :include, PluginTemplate
How to create a catch-all exception handler for Rails
Ruby posted about 1 year ago by christian
Rails’ exception handling is defined in this file /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/rescue.rb. To intercept all errors in your Rails app, you can do this:
1 module GulagNotifier 2 def self.included(base) 3 if base.instance_methods.include?("rescue_action_in_public") 4 base.module_eval { alias_method_chain :rescue_action_in_public, :gulag } 5 end 6 if base.instance_methods.include?("rescue_action_locally") 7 base.module_eval { alias_method_chain :rescue_action_locally, :gulag } 8 end 9 end 10 11 def rescue_action_locally_with_gulag(exception) 12 send_to_gulag(exception) 13 rescue_action_locally_without_gulag(exception) 14 end 15 16 def rescue_action_in_public_with_gulag(exception) 17 send_to_gulag(exception) 18 rescue_action_in_public_without_gulag(exception) 19 end 20 21 def send_to_gulag(exception) 22 # Do whatever you want with the exception 23 end 24 25 end