Showing posts with label learn ruby on rails. Show all posts
Showing posts with label learn ruby on rails. Show all posts

Monday, March 2, 2009

Easy Exceptions handling in ROR

Use this in your application.rb file :
rescue_from NameError, :with => :handle_exceptions
rescue_from NoMethodError, :with => :handle_exceptions
rescue_from ActiveRecord::Rollback, :with => :handle_exceptions
rescue_from ActiveRecord::StatementInvalid, :with => :handle_exceptions
rescue_from ActiveRecord::RecordNotFound, :with => :handle_exceptions
rescue_from ActionController::UnknownAction, :with => :handle_exceptions
rescue_from ActiveRecord::RecordNotFound, :with => :handle_exceptions
rescue_from ActiveRecord::StaleObjectError, :with => :handle_exceptions
rescue_from ActiveRecord::RecordInvalid, :with => :handle_exceptions
rescue_from ActiveRecord::RecordNotSaved, :with => :handle_exceptions
rescue_from ActionController::MethodNotAllowed, :with => :handle_exceptions
rescue_from ActionController::MethodNotAllowed, :with => :handle_exceptions
rescue_from ActionController::InvalidAuthenticityToken, :with => :handle_exceptions

private

def handle_exceptions
render :action => '500.html',:text => 'This is an error', :status => 500
end

Display keys and values of hashes

Well before reading this Post let me tell you that author of this Idea is My friend "Michael Graff"

<% @hash.each do |key, value| %>
<%=h "#{key.to_s} #{value.to_s}" %>
<% end %>

You can iterate through hashes in many various ways, including sorting them:

<% @hash.keys.sort.each do |key| %>
<%=h "#{key.to_s} #{@hash[key]}" %>
<% end %>