Rails 中的 ActiveModel - 视图 - 控制器而不是 ActiveRecord?

发布于 2024-11-24 16:13:48 字数 1607 浏览 0 评论 0原文

我尝试对我的模型使用 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

任何人都可以帮我解决这个问题吗?

非常感谢!

I'm trying to use ActiveModel instead of ActiveRecord for my models because I do not want my models to have anything to do with the database.

Below is my model:

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

And here's my controller:

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

And my view is:

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

But when I load this view in the browser, I am getting an exception:

undefined method 'to_key' for User:0x104ca1b60

Can anyone please help me with this?

Many thanks in advance!

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

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

发布评论

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

评论(2

温柔女人霸气范 2024-12-01 16:13:48

我深入研究 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_keyto_key 方法由 ActiveRecord::AttributeMethods::PrimaryKey< 定义/code>并且由于您没有使用 ActiveRecord,因此没有 to_key 方法。如果您的模型中有一些行为类似于主键的内容,那么您可以定义自己的 to_key 并保留它。

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

as = options[:as]

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

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

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


执行摘要

  • 如果您的模型有一个行为类似于主键的属性,则定义您自己的 to_key 方法来返回该属性。
  • 或者,为 form_for 提供适当的 :as 选项。

I went rooting around the Rails 3.1 source to sort this out, I figured that would be easier than searching anywhere else. Earlier versions of Rails should be similar. Jump to the end if tl;dr.


When you call form_for(@user), you end going through this:

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

And since @user is neither a String nor Object, you go through the else branch and into apply_form_for_options!. Inside apply_form_for_options! we see this:

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
)

Pay attention to that chunk of code, it contains both the source of your problem and the solution. The dom_id method calls record_key_for_dom_id which looks like this:

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

And there's your call to to_key. The to_key method is defined by ActiveRecord::AttributeMethods::PrimaryKey and since you're not using ActiveRecord, you don't have a to_key method. If you have something in your model that behaves like a primary key then you could define your own to_key and leave it at that.

But, if we go back to apply_form_for_options! we'll see another solution:

as = options[:as]

So you could supply the :as option to form_for to generate a DOM ID for your form by hand:

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

You'd have to make sure that the :as value was unique within the page though.


Executive Summary:

  • If your model has an attribute that behaves like a primary key, then define your own to_key method that returns it.
  • Or, supply an appropriate :as option to form_for.
戒ㄋ 2024-12-01 16:13:48

看起来您应该调查(没有很好记录的)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

Looks like you should have instead investigated the (not very well documented) ActiveModel::Conversions class

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

  include ActiveModel::Conversion

  def persisted?
    false
  end

would have done the trick, same applies to Rails 4.2

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