Skip to content

Instantly share code, notes, and snippets.

@TXDynamics
TXDynamics / user.rb
Created April 23, 2013 17:55
Ruby validated email address
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true , format: { with: VALID_EMAIL_REGEX }
@TXDynamics
TXDynamics / application.html.erb
Created April 23, 2013 20:33
Add debugging to our Ruby Application
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
@TXDynamics
TXDynamics / database.yml
Created April 24, 2013 13:24
Sample MySQL Ruby database.yaml file
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
@TXDynamics
TXDynamics / users_controller.rb
Created April 24, 2013 13:33
Code for soving data to a database
# The create def is a default just like show so you must use it
# And you would use it when handling the submission of forms created
# inside of the Controller for users
def create
# This code is run on the submission of our create form
@user = User.new(params[:user])
if @user.save
# This code is executed on a valid save
redirect_to @user
@TXDynamics
TXDynamics / application.html.erb
Created April 24, 2013 13:55
Display temp status messages using a loop and the ruby 'flash' which is a temporary 'hash' object
<!--The Flash item is a temporary space used within Ruby
In this case we are using it to display status messages accross
The ruby site We are using a loop to do it-->
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
@TXDynamics
TXDynamics / en.yml
Created April 24, 2013 14:10
Change name of database column to show better errors
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
# When an active record error message is show it uses the column name for the validation error
# In the case of non English column names we need to create a name alias. In this case we are
# changing the column name password_digest. We are giving it the Alias of 'Password'
en:
activerecord:
attributes:
@TXDynamics
TXDynamics / users_helper.rb
Created April 24, 2013 14:13
Pass a value directly to a class method
# An example of passing a value to a method within a class directly
def gravatar_for(user, options = { size: 50 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "http://www.gravatar.com/avatar/#{gravatar_id}.jpg?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
@TXDynamics
TXDynamics / contact_us.html.erb
Created May 1, 2013 21:50
Pass HTML using provide method back to a view using the .html_safe method
<% provide(:article_heading, 'Contact N&ugrave;R&ugrave;mie'.html_safe) %>
@TXDynamics
TXDynamics / Rails-String-Show-Alternative-if-blank.rb
Created May 21, 2013 19:33
Ruby-Rails-String-Show-Alternate-If-Blank
<%= model.field.blank? ? 'Fill In' : model.field %>
@TXDynamics
TXDynamics / database.yml
Created May 24, 2013 13:09
Ruby-Sample-database.yaml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html