In rails 2, if you want custom messages in ordered style then,
you have to install plugin from
http://orderederrors.rubyforge.org/
and use below lines in views (it's an example)
error_messages_for(:user, :order=>[:name,:email])
error_messages_for(:user, :order=>{:user=>[:name,:email,:contact]})
It will give you messages in ordered but not customized
To customize message you have to change "patch_active_record.rb" file from "lib" directory from "ordered_error_messages_for" plugin by below code:
------------------------------------------------------------------------------------------------------
ActiveRecord::Errors.class_eval do
def full_messages(ordered_keys=nil)
full_messages = []
ordered_keys = [] unless ordered_keys
# First, take the intersection of ordered keys and actual errors.
ordered_keys &= @errors.keys
# Then, add whatever was not specified in the order array.
ordered_keys += @errors.keys-ordered_keys
ordered_keys.each do |attr|
@errors[attr].each do |msg|
next if msg.nil?
if attr == "base"
full_messages << msg
elsif msg =~ /^\^/
full_messages << msg[1..-1]
elsif msg.is_a? Proc
full_messages << msg.call(@base)
else
full_messages << @base.class.human_attribute_name(attr) + " " + msg
end
end
end
full_messages
end
end
------------------------------------------------------------------------------------------------------
Now you have to restart the server
To customize message you can use below lines in model (It's an example)
validates_presence_of :name, :message => "^Hey, name can't be blank"
Now you will get error messages in ordered and customized.
unknown runtime error in prototype.js Line: 2457
this statement in prototype.js (line 2457):
else element.innerHTML = content.stripScripts();
gives an error : "unknown runtime error" in IE
To solve this problem, just make below changes in your code in all js
Change the statement from
this.month_year_label.update(Date.months[m]);
to
this.month_year_label.update().insert(Date.months[m]);
only change is:
instead of using method "update", use "update().insert"
Listing files from a directory with Rails
Code to use in Ruby on Rails:
@files = Dir.glob("path-of-directory/*")
you can check whether any files are there or not ? by the below code,
@total_files = @files.size
Here, you will get all files and folders of the specified directory
If you want only files, then you can use below command,
@files = Dir.glob("path-of-directory/*.*")
Ok. now you can get files names by below code:
for file in @files
puts file
end
If you want random file name and it's extension from @files, then
file_name = @files[rand(999)%@files.size]
ext = file_name[file_name.index('.')+1, file_name.length]
This is the best way I have found for pulling files from a directory and listing them in an array.