views:

108

answers:

2

So I've got a signup form and part of it is billing/credit card info. That info is all stored and processed by a third party app and so the data isn't stored in our database.

So how can I still run the items through the validates_ methods in Rails? (validates_presence_of, validates_length_of, etc etc)

Or am I going about validation in the wrong place for those items?

A: 

You could create a dummy model to facilitate your form's information. There is no requirement to save models to a database.

Just create a new file in app/models and you're pretty much good to go.

example: app/models/ccinfo.rb

class Ccinfo < ActiveRecord::Base

  attr_accessor :card_type, :card_number, :pin, :exp_month, :exp_date, :card_holder, ...

  validates_presence_of :card_holder
  ...

end

If the form is based on another model you can just move the internals of the above model into the model form is based upon, for exactly the same effect.

You can validate without saving by calling the valid? method. Render the errors as you would with any other model if valid? returns false. Otherwise continue processing.

EmFi
A: 

Yes I believe it is possible to do this

for example

 class User < ActiveRecord::Base
     attr_accessor :card_number
     validates_presence_of :card_number
 end

You need to create the attribute in the model itself and then apply the validation to it.

You can also write your own accessor that sends the data to the third party like this

   class User < ActiveRecord::Base
   attr_accessor :card_number
   validates_presence_of :card_number

       def card_number=(number)
         .....
       end
   end
rube_noob