我有一個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問題。
你的控制器代碼是什麼樣的?您將無法保存擁有信用卡的用戶......這是發生故障的地方。 – 2011-04-20 23:41:53
我甚至沒有保存。當您嘗試加載表單時出現此錯誤。我想我只需要添加一些不嵌套的額外表單域。 – Devin 2011-04-21 14:23:06
我不認爲你應該有一個用戶。只需要一張信用卡的表格 – 2011-04-21 14:28:09