nulltek
nulltek

Reputation: 3337

Rails Stripping whitespace before and after fields

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.

Upvotes: 1

Views: 3192

Answers (3)

Joshua Pinter
Joshua Pinter

Reputation: 47481

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.

Upvotes: 0

Allerin
Allerin

Reputation: 1108

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

Upvotes: 3

Federico Piazza
Federico Piazza

Reputation: 30995

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*$/, '')

Upvotes: 0

Related Questions