Rails 中的 ActiveModel - 视图 - 控制器而不是 ActiveRecord?
我尝试对我的模型使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我深入研究 Rails 3.1 源代码来解决这个问题,我认为这比在其他地方搜索更容易。 Rails 的早期版本应该是类似的。如果 tl;dr. 则跳至末尾。
当您调用
form_for(@user)
时,您最终会经历以下过程:由于
@user
既不是字符串也不是对象,所以您会经历else 分支并进入
apply_form_for_options!
。在apply_form_for_options!
中,我们看到了这一点:注意那段代码,它包含问题的根源和解决方案。
dom_id
方法调用record_key_for_dom_id
,如下所示:您可以调用
to_key
。to_key
方法由ActiveRecord::AttributeMethods::PrimaryKey< 定义/code>
并且由于您没有使用 ActiveRecord,因此没有
to_key
方法。如果您的模型中有一些行为类似于主键的内容,那么您可以定义自己的to_key
并保留它。但是,如果我们回到
apply_form_for_options!
,我们会看到另一个解决方案:因此,您可以向
form_for
提供:as
选项来生成手动表单的 DOM ID:不过,您必须确保
: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:And since
@user
is neither a String nor Object, you go through theelse
branch and intoapply_form_for_options!
. Insideapply_form_for_options!
we see this:Pay attention to that chunk of code, it contains both the source of your problem and the solution. The
dom_id
method callsrecord_key_for_dom_id
which looks like this:And there's your call to
to_key
. Theto_key
method is defined byActiveRecord::AttributeMethods::PrimaryKey
and since you're not using ActiveRecord, you don't have ato_key
method. If you have something in your model that behaves like a primary key then you could define your ownto_key
and leave it at that.But, if we go back to
apply_form_for_options!
we'll see another solution:So you could supply the
:as
option toform_for
to generate a DOM ID for your form by hand:You'd have to make sure that the
:as
value was unique within the page though.Executive Summary:
to_key
method that returns it.:as
option toform_for
.看起来您应该调查(没有很好记录的)ActiveModel::Conversions 类
https://github.com/rails/rails/blob/3-1-stable/activemodel/lib/active_model/conversion.rb
就可以了,同样适用于 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
would have done the trick, same applies to Rails 4.2