Forum: Ruby on Rails question about validate

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.
862b20f190d93c4c3649db86de67ee6d?d=identicon&s=25 spmm_pls (Guest)
on 2005-11-12 13:42
(Received via mailing list)
Hi,

I have this class called Myfile (cause file is a reserved word), when a
validation of a field in this class fails the error message shows: "1
error prohibited this myfile from being saved", of course I just want it
to say file instead of myfile. However... I don't have a clue how to do
this.

Any ideas???

Thanks,
Mischa.
862b20f190d93c4c3649db86de67ee6d?d=identicon&s=25 spmm_pls (Guest)
on 2005-11-12 17:20
(Received via mailing list)
> I have this class called Myfile (cause file is a reserved word), when a
> validation of a field in this class fails the error message shows: "1
> error prohibited this myfile from being saved", of course I just want it
> to say file instead of myfile.

OK, I'm getting around it like this now:

<% err = error_messages_for("myfile") %>
<% if err %>
<%= err.sub!("myfile", "file") %>
<% end %>

But there must be any another way. Help!

Grt,
Mischa
Ce60c4f78a63b0695e4dafc4bd7964f7?d=identicon&s=25 vanek (Guest)
on 2005-11-12 20:48
(Received via mailing list)
another (shorter) way:
<% err = ((a=error_messages_for("myfile")).nil? ? nil : a.sub("myfile",
"file")) %>

better yet, you could create a new function in a helper to perform the
transform:

def myfile2file(s)
	return if s.blank?
	s.sub "myfile", "file"
end

<% err = myfile2file(error_messages_for("myfile")) %>
862b20f190d93c4c3649db86de67ee6d?d=identicon&s=25 spmm_pls (Guest)
on 2005-11-12 22:58
(Received via mailing list)
> def myfile2file(s)
>     return if s.blank?
>     s.sub "myfile", "file"
> end
>
> <% err = myfile2file(error_messages_for("myfile")) %>

Thanks. Why didn't think of that myself? In the end I implemented it
like this:

def myfile_to_file(msg)
   msg.sub("myfile", "file") if msg
end

<%= myfile_to_file(error_messages_for("myfile")) %>
This topic is locked and can not be replied to.