Forum: Ruby on Rails Parsing validation on model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-12-07 16:01
(Received via mailing list)
hey,
i want to have the validation of a model being parsed in to a string and
added
to a global string. I read an csv file, do validation, and create an
error
message for each record, and add that to the global error message to
display it
later to the user

i used this code, but i dont get it to work

@msg = ""

looping...

error = false
errormsg = ""

employee = Employee.find(:all, :conditions =>['firm_id = ? and
first_name = ?
and last_name = ? ', firm_id, first_name, last_name])
if employee.empty?
	error = false
else
	error = true #name already exist
	errormsg = "Firstname and lastname are already taken."
end

if error
	@msg += "Record: " + first_name + ", " + last_name + ", " + phone + ",
" +
phone_pin + ", " + errormsg + "<br/>"
else
	@tempemployee= Employee.new
	#SETING THE VALUES TO THE OBJECT
	....
	#SAVING THE OBJECT
	if @tempemployee.save
	else
		 err = parse_errors('employee')
                 @msg += "Record: ""saving error" + err + "<br/>"
	end
end

----------------
the method i use
----------------

 def parse_errors(obj, strip=false)
      return nil if obj.nil?
      err = error_messages_for(obj)
      err[/(.*)<p>There were problems with the following
fields:<\/p>(.*)<\/div>/] if !err.nil?
      return "" if $2.nil?
      str = "<b>" + $2.to_s + "</b>"
      str.gsub!(/<ul>/,'');
      str.gsub!(/<\/ul>/,'');
      str.gsub!(/''/, "\\\\'") unless strip==true
      return str
end

-----------------
the error i get
----------------
undefined method `error_messages_for' for
#<EmployeesController:0x3778838>

i need this to work in the controller, not in the rhtml, cuz it is in a
loop.

thanks in advance
Nick
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-12-08 11:21
(Received via mailing list)
i found a solution,

put this in your application.rb

def parse_errors(obj)
	return nil if obj.nil?

	str = "<ul>"
	obj.errors.each_full do |msg|
	str += "<li>" + msg +"</li>"
	end
	str += "</ul>"
	return str
end


to call the method just do

err = parse_errors(@object)
This topic is locked and can not be replied to.