Forum: Ruby on Rails how to cycle over all self.xx elements performing a method?

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.
9bf5defe25030e539ac1cfaa309eda7c?d=identicon&s=25 scot (Guest)
on 2007-02-27 19:17
Hi,

I would like to perform CGI.escapeHTML to all var's before save,
somthing like
(that obviously dont work)
self.each do |attr|
     attr = CGI.escapeHTML(attr) if attr
end

Thanks
Scot
Daa61d3c632216dc6705c648d070ad0f?d=identicon&s=25 Mark Thomas (Guest)
on 2007-02-27 19:25
(Received via mailing list)
> I would like to perform CGI.escapeHTML to all var's before save,

Wouldn't this be a straightforward application of the before_save()
callback in your model(s)?
9bf5defe25030e539ac1cfaa309eda7c?d=identicon&s=25 scot (Guest)
on 2007-02-27 19:30
Mark Thomas wrote:
>> I would like to perform CGI.escapeHTML to all var's before save,
>
> Wouldn't this be a straightforward application of the before_save()
> callback in your model(s)?

yes, this is how I am currently doing it, maybe I have miss understood
how before save works

before_save :encodeTags

#
#removes html tags and encode &'s etc.. CGI.escapeHTML()
def removeTags
        self.name = strip_tags(self.name) if self.name
        self.author = strip_tags(self.author) if self.author
        self.author_email = strip_tags(self.author_email) if
self.author_email
        self.url = strip_tags(self.url) if self.url
        self.repository = strip_tags(self.repository) if self.repository
end

I want to be able just to cycle over each attribute without having to
specify a line for each one.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-02-27 19:41
(Received via mailing list)
Hi Scot,

scot wrote:
> before_save :encodeTags
> def removeTags
> end
I assume you recognize the mismatch here and that it's a typo.

> I want to be able just to cycle over each attribute
> without having to specify a line for each one.

Scaffold a sandbox app over a table and look at the list method to see
how
Rails generates the column headings.  That's how you do it.

hth,
Bill
1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-02-27 19:58
(Received via mailing list)
> I want to be able just to cycle over each attribute without having to
> specify a line for each one.
not tested, just writting by heart but i'm pretty sure it should work

self.attributes.each do |key,value|
    self[key] = strip_tags(self[key]) if self[key]
end

regards,

javier ramirez

--
--------
Estamos de estreno... si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!
C237cf537a06b60921c97804679e3b15?d=identicon&s=25 John Barnette (Guest)
on 2007-02-27 20:03
(Received via mailing list)
# Maybe something like...
attribute_names.select { |a| attribute_present? a }.each { |a| self[a]
= strip_tags(self[a]) }
9bf5defe25030e539ac1cfaa309eda7c?d=identicon&s=25 scot (Guest)
on 2007-02-27 20:29
John Barnette wrote:
> # Maybe something like...
> attribute_names.select { |a| attribute_present? a }.each { |a| self[a]
> = strip_tags(self[a]) }

thanks everyone, this is how I did it in the end, however teh above
solution seems to look nice too.

#
# add new lines as required
# CGI.escapeHTML() or ERB::Util::html_escape
def encodeTags

string_attributes = {}
  self.attributes.each do |each_name, each_attribute|
     if each_attribute.kind_of? String
        string_attributes[each_name] =
ERB::Util::html_escape(each_attribute)
     end
  end
self.attributes = string_attributes

end
This topic is locked and can not be replied to.