1

I have a Rails 3.2.14 app where I collect data values through a typical form. There are times when staff members will enter a field such as name as " Doe, John " Where the format should be "Doe, John". Or in other instances they may add an address to the form such as " 111 W. 8th St Houston, TX 77448 " instead of "111 W. 8th St Houston, TX 77448".

So basically they are doing a lot of cutting and pasting which at times includes leading and trailing whitespace.

I'm somewhat familiar with strip but I'm not sure now I can strip just the leading and trailing whitespace.

I'd like to make this a validation or a callback before_save or before_create filter.

If anyone has any advice on how to strip the leading/trailing whitespaces and what that validation or method would look like I'd appreciate it.

nulltek
  • 3,247
  • 9
  • 44
  • 94

3 Answers3

3

Have a look at attribute_normalizer

You can very easily use it as following

class User < ActiveRecord::Base

  normalize_attribute :first_name, :with => :strip

end

Also there are few other options available like for boolean values, phone number or mobile

You can use normalizer for multiple attributes as

class User < ActiveRecord::Base

  normalize_attribute :first_name, :last_name, :email, :with => :strip

end

This has very clean syntax for use.

Edit:

attribute_normalizer also provides some more options

  • :squish => same as squish
  • :phone => removes non-digits

You can also define your custom normalizers in the application

Allerin
  • 1,108
  • 10
  • 7
  • Here's a gist of what I was trying to do with `before_save` https://gist.github.com/teknull/250525117e781bf48ec1 – nulltek Sep 17 '14 at 17:36
  • @cz3ch use this answers, that easy way. – Roman Kiselenko Sep 17 '14 at 17:37
  • I just installed and setup attribute_normalizer and used the example to squish fields. However if there are two consecutive spaces in a field such as `111 8th St Houston, TX 77448` it does not remove the extra spaces like squish does. Any idea as to why? – nulltek Sep 17 '14 at 17:46
  • @cz3ch to squish you can simply use `normalize_attribute :facility_name, :facility_address, :facility_phone, :with => :squish` – Allerin Sep 17 '14 at 17:50
  • @Allerin I have it setup and it does remove the leading/trailing whitespaces. But if there are extra spaces between characters it does not squish/remove that. Any thoughts as to why? Thanks for all your help btw! – nulltek Sep 17 '14 at 17:53
  • @cz3ch checking in the code [SquishNormalizer](https://github.com/mdeering/attribute_normalizer/blob/master/lib/attribute_normalizer/normalizers/squish_normalizer.rb) it strips and then with regex it squishes extra spaces. It is working well at my end. Could you please recheck for option passed to normalizer? – Allerin Sep 17 '14 at 18:05
  • @Allerin Here's what I'm passing in my model: `normalize_attribute :facility_name, :facility_address, :facility_contact, :facility_phone, :with => :strip` It strips the leading/trailing spaces but not the consecutive spaces. – nulltek Sep 17 '14 at 18:07
  • @cz3ch You have passed `:with => :strip` instead pass `:with => :squish`. – Allerin Sep 17 '14 at 18:12
  • @Allerin OMG I'm so sorry about that. Using `:with => :squish` works great. I'm running on 2 hours of sleep so I need to be more careful. :) Thanks for your help. You solved a big problem for me. – nulltek Sep 17 '14 at 18:20
0

You could use strip. As the documentation says:

strip → new_str
Returns a copy of str with leading and trailing whitespace removed.

"    hello    ".strip   #=> "hello"
"\tgoodbye\r\n".strip   #=> "goodbye"

So, strip should help you.

However, if you want to use a regex to do this. You could use:

^\s*|\s*$

Working demo

enter image description here

So, your code could be:

str.gsub!(/^\s*|\s*$/, '')
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

StripAttributes Gem

This is what you're looking for for hassle-free, safe striping of leading and training whitespace.

Basic Usage

  1. Install the gem into your Gemfile:

    gem 'strip_attributes', '1.8.0'  # Strips leading and trailing whitespaces in form input.
    
  2. Add it to any models you accept form (or API) input from your users:

    class YourModel < ActiveRecord::Base
    
      strip_attributes
    
    end
    

    ProTip: If you have a Base class that your other models inherit from, put it in there.

By default, this will strip all leading and training whitespaces from all attributes. There are other options you can use to limit what attributes it is applied to and whether multiple spaces between words are collapse into a single space, etc.

But, the default is excellent and very safe. I find this better than the alternatives that require you to specify which attributes you want to strip. Generally, you want to strip all attributes.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245