Notes posted by ncancelliere

RSS feed
October 16, 2009 - (>= v2.1.0)
1 thank

No numbers or symbols

“Kyle”, “Дети”, “Niños”, “Quan-lu”, “た ち”

validates_format_of :first_name, :with => /^([^\d\W]|[-])*$/
January 15, 2009 - (v1.2.6 - v2.2.1)
0 thanks

Convert String to Class

this example shows how you can take a string and # make it a class and then send it a method

Kernel.const_get(my_string.capitalize).select_options 
January 15, 2009 - (v1_8_6_287)
1 thank

Convert String to Class

this example shows how you can take a string and make it a class and then send it a method

Kernel.const_get(my_string.capitalize).select_options
November 16, 2008 - (>= v2.1.0)
11 thanks

Application Helper for Fading Flash Messages

A simple helper method for showing the flash message. Includes optional fade in seconds (view needs javascript_include_tag defaults if you desire fade effect):

def show_flash_message(options={})
  html = content_tag(:div, flash.collect{ |key,msg| content_tag(:div, msg, :class => key) }, :id => 'flash-message')
  if options.key?(:fade)
    html << content_tag(:script, "setTimeout(\"new Effect.Fade('flash-message');\",#{options[:fade]*1000})", :type => 'text/javascript')
  end
  html
end

simply call in your views then using:

<%= show_flash_message(:fade => 4) %>
August 28, 2008
10 thanks

Get year to show in descending order (Today to 1920 for example)

The way people think of time start and end would be 1920 to today. This made me think “but I want it show the current year first then down.” Well it’s as simple as swapping the start_year and end_year.

date_select :date, :start_year => Date.current.year, :end_year => 1920

# => 2008, 2007, 2006, 2005 ... 1920
August 26, 2008
6 thanks

Prototype hinted_text_field application_helper

Place this in your helper. It will show a message inside the text box and remove it when someone clicks on it. If they don’t enter a value when they leave the field it’ll replace the message. (Requires javascript :defaults).

def hinted_text_field_tag(name, value = nil, hint = "Click and enter text", options={})
  value = value.nil? ? hint : value
  text_field_tag name, value, {:onclick => "if($(this).value == '#{hint}'){$(this).value = ''}", :onblur => "if($(this).value == ''){$(this).value = '#{hint}'}" }.update(options.stringify_keys)
end

# inside form_for example

hinted_text_field_tag :search, params[:search], "Enter name, brand or mfg.", :size => 30  
# => <input id="search" name="search" onblur="if($(this).value == ''){$(this).value = 'Enter name, brand or mfg.'}" onclick="if($(this).value == 'Enter name, brand or mfg.'){$(this).value = ''}" size="30" type="text" value="Enter name, brand or mfg." />
August 22, 2008 - (>= v2.1.0)
0 thanks

This is a private method

This is not a public method, but a private class method.

>> Person.included

NoMethodError: private method `included’ called for #<Class:0x233f084>