# Trim all input fields

**URL:** <https://www.ruby-forum.com/t/trim-all-input-fields/164006>\
**Category:** Rails\
**Created:** [March 26, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/trim-all-input-fields/164006 "2009-03-26T20:39:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikhilvaghela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@nikhilvaghela](https://www.ruby-forum.com/u/nikhilvaghela)\
**Post date:** [March 26, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/trim-all-input-fields/164006/1 "2009-03-26T20:39:26Z")

</div>

Hi all,

What is the easiest way to trim all input fields before validation.

Also on error the field should show the trimed version of the data.

Any help would be appreciated, Thanks.

---

<div class="post-metadata">

**Author:** ![nikhilvaghela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@nikhilvaghela](https://www.ruby-forum.com/u/nikhilvaghela)\
**Post date:** [March 26, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/trim-all-input-fields/164006/2 "2009-03-26T21:01:04Z")

</div>

On Thu, Mar 26, 2009 at 2:39 PM, Nikhil Vijayan  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> What is the easiest way to trim all input fields before validation.
> 
> Also on error the field should show the trimed version of the data.
> 
> Any help would be appreciated, Thanks.

Append a new feature to your models:

module Trimmer  
def self.append\_features( base )  
base.before\_validation do |model|  
model.attribute\_names.each do |n|  
u[n] = u[n].strip if u[n].respond\_to?( ‘strip’ )  
end  
end  
end  
end

require “#{ RAILS\_ROOT }/app/models/trimmer”  
class ActiveRecord::Base  
include Trimmer  
end

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![nikhilvaghela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@nikhilvaghela](https://www.ruby-forum.com/u/nikhilvaghela)\
**Post date:** [March 26, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/trim-all-input-fields/164006/3 "2009-03-26T21:22:15Z")

</div>

On Thu, Mar 26, 2009 at 2:59 PM, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> u[n] = u[n].strip if u[n].respond\_to?( ‘strip’ )

Sorry, this line should actually be:

model[n] = model[n].strip if model[n].respond\_to?( ‘strip’ )

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![nikhilvaghela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@nikhilvaghela](https://www.ruby-forum.com/u/nikhilvaghela)\
**Post date:** [March 26, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/trim-all-input-fields/164006/4 "2009-03-26T22:15:36Z")

</div>

Thanks Greg,

Is there anyway to change this to something better ?

require “#{ RAILS\_ROOT }/app/models/trimmer”

is there any way to move it to plugin or helper or somewhere else and  
load it in a better way … thanks …

---

<div class="post-metadata">

**Author:** ![nikhilvaghela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@nikhilvaghela](https://www.ruby-forum.com/u/nikhilvaghela)\
**Post date:** [March 26, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/trim-all-input-fields/164006/5 "2009-03-26T22:27:25Z")

</div>

On Thu, Mar 26, 2009 at 4:15 PM, Nikhil Vijayan  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Greg,
> 
> Is there anyway to change this to something better ?
> 
> require “#{ RAILS\_ROOT }/app/models/trimmer”
> 
> is there any way to move it to plugin or helper or somewhere else and  
> load it in a better way … thanks …

You can put it wherever you want it. Seemed like a model-like thing  
so I put it in with the models.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
