2
votes

I have a Rails 3 form (simple_form, really) that has a set of nested attributes:

<%= simple_form_for(@user, :url => the_path(@user)) do |f| %>
  ...
  <%= f.simple_fields_for :credit_card do |c| %>
    <%= c.input :number, :label => 'Credit card number' %>
      ...
  <% end %>
...
<% end %>

The problem is that the :credit_card attributes belong to the class CreditCard, which is not a model since I'm not storing any of the credit card data in the database. I have that Class defined in /app/models/credit_card.rb like this (per this RailsCast):

class CreditCard
  include ActiveModel::Validations
  include ActiveModel::Conversion
  include ActiveModel::Naming

  attr_accessor :number, :expiration_month, :expiration_year, :cvv

  validates_presence_of :number, :expiration_month, :expiration_year, :cvv

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

In user.rb, I have this:

  has_one :credit_card
  accepts_nested_attributes_for :credit_card

When I access the page, I get this error:

undefined method `quoted_table_name' for CreditCard:Class

Googling that error didn't yield any suggestions. I'm able to create CreditCard objects from Rails Console, but for some reason the Rails form generator isn't seeing the class.

I already tried swapping out simple_form_for with form_for (and the related changes), so I don't think it's a simple_form problem.

3
What does your controller code look like? You won't be able to save a user that has a credit-card... that's where this breaks down.Jesse Wolgamott
I'm not even getting to the save yet. This error appears when you try to load the form. I suppose I'll just have to add some extra form fields that aren't nested.Devin
I don't think you should have a user at all. Just have a form for the credit-cardJesse Wolgamott
The user model also acts_as_authentic and stores contact and shipping information, so I do need it. I just wanted to add these additional CC fields to use with our payment gateway. For now, I'll just remove the non-model CreditCard class and put some additional form fields on there with attr_accessor.Devin
You can pre-populate contact/shipping into credit-card.. But you cannot have nested_attributes_for a non-database backed model.Jesse Wolgamott

3 Answers

1
votes

This seems error in the association with user

1
votes

This seems to be a problem with having a model without an associated table in the database. I am having the same situation where I don't want to store a nested model in application database table but rather fetch and save information to and from an external service.

class Order < ActiveRecord::Base
  attr_accessor :identifier, :amount
  has_one :credit_card_transaction
end

class CreditCardTransaction
  attr_accessor :number, :expiration_date
  belongs_to :order
end

I don't want to save credit card transaction object in local database. And getting the same problem as you are. Please keep posted for any updates.

0
votes

It may be a case of the presence of another class that has the same name "CreditCard" which in this case the class is not an ActiveRecord and thus does not have quoted_table_name method.