Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Ed Erx

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

multiple gets entry (ruby)

// This code isn't mine
// I found this code at http://groups.google.com/group/malaysia-rb/browse_thread/thread/4e69834b49df055b
puts 'enter'
names = []
i =0
while names[i-1] != 'end'
   names.push gets.chomp
   i = i+1
end
 i=i-1
names.each do |count|
 if i != 0
   puts count
 end
 i=i-1
end 

Load select box with model

This will generate a select box loaded with parent model

select :parent_id, Parent.find(:all).collect{|p| [p.full_name, p.id]}, {:include_blank => true}

validate parent model

Validate a parent model in child model
based on a post at http://www.railsweenie.com/forums/1/topics/439
question : can this be improvised?

Book (parent)
class Book < ActiveRecord::Base
  has_many :pages
end


Pages (children)
class Pages < ActiveRecord::Base
  belongs_to :book
  
  validates_presence_of :page_id # this is not enough, anything can go in this field

  # we check if the parent object is valid or not
  validate do |page|
   if page.blank?
     unless page.book(true)
       errors.add(:page_id, "must be a valid Book id")
     end
   end
  end

end

passing parameters into before filter

application.rb
class ApplicationController < ActionController::Base
 
  def hello(name)
   "Hello #{name}"
  end

end


users_controllers.rb
class UsersController < ApplicationController

  before_filter :only => :index do |u| 
    u.hello('Master')
  end

end
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS