Birds-Eye.Net
All things broadband and more...
 
Web Birds-Eye.Net

What's New?

Ruby on Rails (RoR)
Programming Reference


Models
External database connections
Passing current_user into model
Passing object into model
Using static lookup tables
Validates IF
Validates MongoMapper

Views
Dynamically delete form element
Edit create nested data
HTML form field check_box
Layout jQuery datatable module
Select array
Select cascading via JS
Text_area Array
Text_area listing submit
Text field format time

Controllers
Dynamic model selection
Including first item from a sorted desc table
Using from_unixtime on epoch dates
Custom SQL Query Examples

Rack
Integrated NTLM/Kerberos Authentication
Pass-through Authentication w/ NTLM

ActionMailer
Broken links in emails

Rails General
Add, Subtract, Multiply, and Divide
Calculate number of weekdays for date range
Date->Epoch & Epoch->Date
Calculate past/present payroll dates
Extract first letter of each word
Hash of hashes assignment
Using: variable as hash index

jQuery
jQuery accordion MongoDB

Rails Framework Examples

Apotomo Widget Using Erb

MySQL
Converting Julian Dates to Epoch

d3 Charting
Configuration to Work with Rails Apps
A simple bar chart example

Other
Setup VPN on iMac
SSH Key Generation

More to come

 

Use of Conditional (IF) in Model Validations

By: Bruce Bahlmann - Contributing Author (your feedback is important to us!)

In rails development, when creating web forms, you often have a particular field dependent on another. For example, lets say you have a checkbox (boolean) input value whose value controls the need for an additional text input value. As such, when adding validation to to these fields within the model, you must either make these fields optional, or if you want to make them dependent on the setting of this checkbox (used in this particular example) you need to use a conditional.

Fortunately, Ruby on Rails (RoR) provides an easy way to validate these without creating a "custom validation" (which is an alternative way to do this, albeit more complicated). Simply place the [:if] after the validates entry for the field which is dependent on another input field.

Class Post < ActiveRecord::Base
  Validates :<field name>, :presence => true, :if => "<field checked> == true"
end

The above entry with in app/models/post.rb reads as follows. Validates would require the presence of a value within the <field name> (name of the text input field) IF the <field check> (the name of the checkbox input field) was set to true.

Note: a checkbox field is not "nil", rather it is either true if checked, or false if either not checked or not set.

Can Birds-Eye.Net help you or your Company?
Receive your Birds-Eye.Net articles and white papers hot off the presses by adding our RSS feed to your reader.

 

(C) Copyright Birds-Eye.Net, All rights reserved.
It is against the law to reproduce this content or any portion of it in any form without the explicit written permission of Birds-Eye Network Services, LLC. Federal copyright law (17 USC 504) makes it illegal, punishable with fines up to $100,000 per violation plus attorney's fees.