2011-04-20 31 views
2

我有一個Rails 3形式(simple_form,真),其具有一組嵌套屬性:導軌3的形狀誤差: 「未定義的方法`quoted_table_name'」

<%= 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 %> 

的問題是,所述:CREDIT_CARD屬性屬於到CreditCard類,這不是一個模型,因爲我沒有在數據庫中存儲任何信用卡數據。我在/app/models/credit_card.rb定義像這樣的類(每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 

在user.rb,我有這樣的:

has_one :credit_card 
    accepts_nested_attributes_for :credit_card 

當我訪問該網頁,我得到這個錯誤:

undefined method `quoted_table_name' for CreditCard:Class 

使用谷歌搜索錯誤沒有產生任何建議。我能夠從Rails控制檯創建CreditCard對象,但由於某些原因,Rails表單生成器沒有看到該類。

我已經試着用form_for(和相關的改變)換掉了simple_form_for,所以我不認爲這是一個simple_form問題。

+0

你的控制器代碼是什麼樣的?您將無法保存擁有信用卡的用戶......這是發生故障的地方。 – 2011-04-20 23:41:53

+0

我甚至沒有保存。當您嘗試加載表單時出現此錯誤。我想我只需要添加一些不嵌套的額外表單域。 – Devin 2011-04-21 14:23:06

+0

我不認爲你應該有一個用戶。只需要一張信用卡的表格 – 2011-04-21 14:28:09

回答

1

關聯這似乎是與具有模型,而不會在該數據庫中相關聯的表的一個問題。我有同樣的情況,我不想在應用程序數據庫表中存儲嵌套模型,而是從外部服務獲取信息並將信息保存到外部服務。

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 

我不想在本地數據庫中保存信用卡交易對象。和你一樣得到同樣的問題。請保留髮布的任何更新。

0

它可能是一個存在另一個具有相同名稱「CreditCard」的類,在這種情況下,該類不是ActiveRecord,因此沒有quoted_table_name方法。

相關問題