Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rapha@momo:~/Tippspiel/Trunk$ script/console
- Loading development environment.
- *** init.rb
- *** custom_error_message.rb
- >> ue = UserExtension.new
- => #<UserExtension:0xb7182114 @attributes={"team_id"=>nil, "forename"=>"", "t_false"=>nil, "former_place"=>0, "points"=>nil, "nick"=>"", "scorer_id"=>nil, "comp"=>"", "gender"=>"", "cust_no"=>"", "tend"=>nil, "t_true"=>nil, "departm"=>"", "position"=>"", "pos"=>nil, "surname"=>""}, @new_record=true>
- >> ue.save
- => false
- >> ue.errors.full_messages
- => ["Forename ^can't be blank", "Nick can't be blank", "Surname can't be blank"]
- >> module ActiveRecord
- >> class Errors
- >>
- ?> # Redefine the ActiveRecord::Errors::full_messages method:
- ?> # Returns all the full error messages in an array. 'Base' messages are handled as usual.
- ?> # Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
- ?> # in which case the attribute name is omitted.
- ?> # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
- ?> def full_messages
- >> fm = []
- >>
- ?> @errors.each_key do |attr|
- ?> @errors[attr].each do |msg|
- ?> next if msg.nil?
- >>
- ?> if attr == "base"
- >> fm << msg
- >> elsif msg =~ /^\^/
- >> fm << msg[1..-1]
- >> else
- ?> fm << @base.class.human_attribute_name(attr) + " " + msg
- >> end
- >> end
- >> end
- >>
- ?> return fm
- >> end
- >> end
- >> end
- => nil
- >> ue.errors.full_messages
- => ["can't be blank", "Nick can't be blank", "Surname can't be blank"]
- >>
Add Comment
Please, Sign In to add comment