Search code examples
ruby-on-railssimple-form

rails simple_form fields not related to the model


I have an existing form which is tied to a model named 'Order', but i want to add new form fields that will capture Credit Card info such as name, cc number, etc to be processed on a 3rd party payment gateway.

But since i don't want to save CC info in our database, there are no corresponding columns of that in my order table. And this gives me an error when submitting the form that those Credit card input fields are not 'part' of the order model.


Solution

  • You can use attr_accessor

     class Order < ActiveRecord::Base
    
       attr_accessor :card_number
    
    
     end
    

    Now you can do Order.first.card_number = '54421542122' or use it in your form or whatever else you need to do.

    See here for ruby docs http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-attr_accessor and here for a useful stackoverflow question What is attr_accessor in Ruby?

    Don't get it mixed up with attr_accessible! Difference between attr_accessor and attr_accessible