Rails 3 表单错误:“未定义方法‘quoted_table_name’”
我有一个 Rails 3 表单(实际上是 simple_form),它有一组嵌套属性:
<%= 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 Console 创建 CreditCard 对象,但由于某种原因,Rails 表单生成器看不到该类。
我已经尝试用 form_for 替换 simple_form_for (以及相关的更改),所以我不认为这是 simple_form 问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎与用户关联存在错误
This seems error in the association with user
这似乎是数据库中没有关联表的模型的问题。我遇到了同样的情况,我不想在应用程序数据库表中存储嵌套模型,而是从外部服务获取和保存信息。
我不想将信用卡交易对象保存在本地数据库中。并遇到和你一样的问题。请随时发布任何更新。
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.
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.
可能存在另一个具有相同名称“CreditCard”的类,在这种情况下,该类不是 ActiveRecord,因此没有 Quoted_table_name 方法。
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.