Skip to content

Instantly share code, notes, and snippets.

@TXDynamics
TXDynamics / application_helper.rb
Created April 19, 2013 15:26
Ruby Code for a Dynamic Title with a Local Parameter of :title in application_helper
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
@TXDynamics
TXDynamics / application_helper.rb
Last active December 16, 2015 10:39
Ruby Example pass parameter to helper class
#Displays the current date but repeat x times
def display_time(how_many_times)
p = how_many_times.to_i
return p.times {puts Time.now.strftime("%m/%d/%y")}
end
@TXDynamics
TXDynamics / array_sort.rb
Created April 19, 2013 18:42
Ruby Sorting Arrays temporarily and then permanently
user_input = "foo,bar,you,know".split(',') # We use split to make an array from user input
user_input.count # this will display 4 because we store 4 values in our array
# Now we use the temp array sort method
user_input.sort
# To save the values back to the array you need to use !
user_input.sort!
@TXDynamics
TXDynamics / home.html.erb
Created April 22, 2013 17:26
Rails Create an image with link using the image helper
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org' %>
@TXDynamics
TXDynamics / application.html.erb
Created April 22, 2013 18:08
Render Rails Shims. Same technique for using includes in php views
<!--The render command is used by rails to call shims and render them inside of your views-->
<!--shims have to be named the following _shimname.html.erb-->
<%= render 'layouts/header' %>
@TXDynamics
TXDynamics / routes.rb
Last active December 16, 2015 12:49
Creating Rails custom URLS
# Creating better Routes. In rails you match the URI to the controller
# and then the action within the controller, this allows you to make bases URLs
# This creates a route that can also be referenced within code as the following
# contact_path = /contact
# contact_url = http://localhost/contact/
match '/contact', to 'static_pages#contact'
@TXDynamics
TXDynamics / routes.rb
Created April 22, 2013 18:51
Creating the route to the index of your domain
#To set the root of your app delete the index.html file within the
#public folder then use the below
root to: 'static_pages#home'
@TXDynamics
TXDynamics / static_pages_spec.rb
Created April 22, 2013 20:59
Rspec test shortened by naming the subject and using a pre-visit
# My first application test file using RSPEC and Capybara
# Here at the top we are defining the subject which is just pages.
# pages is the normal object that we would call before like pages.have_content
require 'spec_helper'
describe "Static pages" do
subject { page }
@TXDynamics
TXDynamics / Gemfile
Created April 22, 2013 21:20
Annotate your models with database information
# We use the gem annotate so that we annotate our models in development
# After creating your model run "bundle exec annotate --position before"
gem 'annotate', '~> 2.5.0'
@TXDynamics
TXDynamics / model-info.rb
Last active December 16, 2015 12:59
Active Record Transactions for manipulating the database
# You can define any model in the singular like User
# this should match the DB table users in the plural form
# then you can access that as a model as the following object
# When you want the first user in the DB table
user = User.first
# Then you can access the attributes by specifying the column
puts user.name