Skip to content

Instantly share code, notes, and snippets.

@gertig
Created February 1, 2011 04:53
Show Gist options
  • Save gertig/805430 to your computer and use it in GitHub Desktop.
Save gertig/805430 to your computer and use it in GitHub Desktop.
Snippets of code that are very useful in .rb files in Rails
#application_controller.rb
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = "Access Denied"
redirect_to root_url
end
#application_helper.rb
def javascript(*files)
content_for(:head) { javascript_include_tag(*files)}
end
def title(page_title)
content_for(:title) { page_title }
end
def gertig_js_header(js_ph_file) #, js_ap_file)
content_for(:head) {
javascript_include_tag js_ph_file
#javascript_include_tag js_ap_file
}
end
#user.rb
#Creates Single reference for Username
def full_name
"#{first_name} #{last_name}"
end
#Conditional Validation (Episode 41)
def should_validate_password?
updating_password || new_user
#self.role != 'admin'
#current_user.role == 'player'
end
###This is to account for uppercase in emails, especially because Heroku and PostgreSQL are case sensitive and SQLite is not
before_save do
self.email.downcase! if self.email
end
def self.find_for_authentication(conditions)
conditions[:email].downcase!
super(conditions)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment