Rails 3 表单错误:“未定义方法‘quoted_table_name’”

发布于 2024-11-02 17:43:02 字数 1366 浏览 1 评论 0原文

我有一个 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 问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔落惊风雨 2024-11-09 17:43:02

这似乎与用户关联存在错误

This seems error in the association with user

橙幽之幻 2024-11-09 17:43:02

这似乎是数据库中没有关联表的模型的问题。我遇到了同样的情况,我不想在应用程序数据库表中存储嵌套模型,而是从外部服务获取和保存信息。

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

我不想将信用卡交易对象保存在本地数据库中。并遇到和你一样的问题。请随时发布任何更新。

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.

南城旧梦 2024-11-09 17:43:02

可能存在另一个具有相同名称“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文