15

我正在尝试对我的模型使用 ActiveModel 而不是 ActiveRecord,因为我不希望我的模型与数据库有任何关系。

下面是我的模型:

class User
  include ActiveModel::Validations
  validates :name, :presence => true
  validates :email, :presence => true
  validates :password, :presence => true, :confirmation => true

  attr_accessor :name, :email, :password, :salt
  def initialize(attributes = {})
    @name  = attributes[:name]
    @email = attributes[:email]
    @password = attributes[:password]
    @password_confirmation = attributes[:password_confirmation]
  end
end

这是我的控制器:

class UsersController < ApplicationController
  def new
    @user = User.new
    @title = "Sign up"
  end
end

而我的观点是:

<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
<div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</div>
<div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
</div>
<div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
</div>
<div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %>
</div>
<div class="actions">
    <%= f.submit "Sign up" %>
</div>
<% end %>

但是当我在浏览器中加载这个视图时,我得到了一个异常:

undefined method 'to_key' for User:0x104ca1b60

谁能帮我解决这个问题?

提前谢谢了!

4

2 回答 2

33

我围绕 Rails 3.1 源代码进行了整理,我认为这比在其他任何地方搜索更容易。早期版本的 Rails 应该是类似的。如果 tl;dr. 跳到最后。


当你打电话时form_for(@user),你结束了这个:

def form_for(record, options = {}, &proc)
  #...
  case record
  when String, Symbol
    object_name = record
    object      = nil
  else
    object      = record.is_a?(Array) ? record.last : record
    object_name = options[:as] || ActiveModel::Naming.param_key(object)
    apply_form_for_options!(record, options)
  end

由于@user既不是字符串也不是对象,因此您将通过else分支进入apply_form_for_options!. 在里面apply_form_for_options!我们看到:

as = options[:as]
#...
options[:html].reverse_merge!(
  :class  => as ? "#{as}_#{action}" : dom_class(object, action),
  :id     => as ? "#{as}_#{action}" : dom_id(object, action),
  :method => method
)

注意那段代码,它包含问题的根源和解决方案。该dom_id方法调用record_key_for_dom_id如下所示:

def record_key_for_dom_id(record)
  record = record.to_model if record.respond_to?(:to_model)
  key = record.to_key
  key ? sanitize_dom_id(key.join('_')) : key
end

你来电了to_key。该to_key方法由定义,ActiveRecord::AttributeMethods::PrimaryKey并且由于您没有使用 ActiveRecord,因此您没有to_key方法。如果您的模型中有一些行为类似于主键,那么您可以定义自己的to_key并保留它。

但是,如果我们回到过去,apply_form_for_options!我们会看到另一种解决方案:

as = options[:as]

因此,您可以提供手动为表单生成 DOM ID的:as选项:form_for

<%= form_for(@user, :as => 'user_form') do |f| %>

不过,您必须确保该:as值在页面中是唯一的。


执行摘要

  • 如果您的模型有一个行为类似于主键的属性,则定义您自己to_key的返回它的方法。
  • 或者,为 提供适当的:as选项form_for
于 2011-07-17T05:45:42.107 回答
1

看起来您应该改为调查(没有很好记录的)ActiveModel::Conversions 类

https://github.com/rails/rails/blob/3-1-stable/activemodel/lib/active_model/conversion.rb

  include ActiveModel::Conversion

  def persisted?
    false
  end

会成功的,同样适用于 Rails 4.2

于 2016-06-14T21:49:06.120 回答