# Rails 3 / ActiveModel for Credit Card information?

**URL:** <https://www.ruby-forum.com/t/rails-3-activemodel-for-credit-card-information/193229>\
**Category:** Rails\
**Created:** [August 13, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/rails-3-activemodel-for-credit-card-information/193229 "2010-08-13T19:17:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [August 13, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/rails-3-activemodel-for-credit-card-information/193229/1 "2010-08-13T19:17:58Z")

</div>

When submitting billing information in the past I’ve always used  
attr\_accessor for credit card details as they should not be saved in  
the database. In addition I always end up storing the card  
expiration date so that the date form helper works correctly.

With Active Model it seems logical to create a CreditCard class to  
hold this data instead.

**1st issue.**

It seems there still isn’t support for handling the form date\_select.  
(the card\_expires\_on(\*) parameters). Should this work? Perhaps I’m  
just missing an ActiveModel module

**2nd issue.**

In a perfect world this CreditCard class would be on the same form as  
an ActiveRecord Order class but I have not been able to get an  
ActiveModel to work as a nested object on a form.

something like this… on the order new form  
\<%= form\_for @order do |f| -%\>  
. order stuff here…  
\<% f.fields\_for :credit\_card do |cc\_fields| %\>  
\<%= cc\_fields.label :card\_number, ‘\*Number’ %\>  
\<%= cc\_fields.text\_field :card\_number, :class =\>  
‘text’, :autocomplete =\> ‘off’ %\>  
\<% end %\>

I have not found what magic I need to add to the order model to make  
this work.

The \<% f.fields\_for :credit\_card do |cc\_fields| %\> block is never  
executed

if credit card was an ActiveRecord I would simply put  
has\_one :credit\_card  
accepts\_nested\_attributes\_for :credit\_card

instead I added a  
attr\_accessor :credit\_card to the Order ActiveRecord class

and my credit\_card class looks like this…

```
class CreditCard

  include ActiveModel::Validations
  include ActiveModel::AttributeMethods
  include ActiveModel::Callbacks
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  # belongs_to :user

```

attr\_accessor :card\_type, :card\_number, :card\_verification,  
:card\_expires\_on, :agree

validates :card\_type, :card\_number, :card\_verification,  
:card\_expires\_on, :agree, :presence  
=\> true

```
  def initialize(attributes = {})
    expire_date = {}
    #raise attributes.inspect
    attributes.each do |name, value|
        send("#{name}=", value)
        #this breaks on date items from date_select
    end
  end

  def persisted?
    false
  end
end

```

Any idea what I am missing?

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [August 13, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/rails-3-activemodel-for-credit-card-information/193229/2 "2010-08-13T20:29:58Z")

</div>

Figured out why my ActiveModel was not displaying…

\<% f.fields\_for :credit\_card do |cc\_fields| %\>

needs to be

\<%= f.fields\_for :credit\_card do |cc\_fields| %\>

I have another nested element on this page that is an ActiveRecord.  
That one gives a deprecation warning. For some reason the fields\_for  
that takes an ActiveModel gives no warning. It just doesn’t work.  
odd.

Anyone know a built in way to handle date\_select parameters from  
ActiveModel?

just FYI… here is part of my view. (addresses is an ActiveRecord  
and credit\_card is an ActiveModel)

```
    <% f.fields_for :credit_card do |cc_fields| %>
      <%= render :partial => "cc_fields", :locals => { :cc_fields

```

=\> cc\_fields} %\>  
\<% end %\>  
\<% f.fields\_for :addresses do |address\_fields| %\>  
\<%= render :partial =\> “address\_fields”, :locals =\>  
{ :address\_fields =\> address\_fields, :use\_org =\> true } %\>  
\<% end %\>

It doesn’t matter what order these are in the address form will always  
display and the credit\_card will not (unless i use \<%=)

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [September 30, 2010, 6:24am UTC](https://www.ruby-forum.com/t/rails-3-activemodel-for-credit-card-information/193229/3 "2010-09-30T06:24:54Z")

</div>

Have you had any luck w this? I am effectively doing the same thing.

I can get my Credit Card form to appear in with in my Order form,  
however if the form doesn’t validate, all my Credit Card info is lost  
and the user has to re-type it in. I believe it has something to do with  
the my Order class (which extends Activerecord::Base) does not:  
accepts\_nested\_attributes\_for :credit\_card

therefore the input names aren’t being created correctly, etc.

Thoughts?
