ActiveModel - 视图 - Rails 中的控制器而不是 ActiveRecord? [英] ActiveModel - View - Controller in Rails instead of ActiveRecord?

查看:53
本文介绍了ActiveModel - 视图 - Rails 中的控制器而不是 ActiveRecord?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是我的模型:

类用户包括 ActiveModel::Validations验证 :name, :presence =>真的验证 :email, :presence =>真的验证 :password, :presence =>真,:确认 =>真的attr_accessor :name, :email, :password, :saltdef 初始化(属性 = {})@name = 属性[:name]@email = 属性[:email]@password = 属性[:密码]@password_confirmation = 属性[:password_confirmation]结尾结尾

这是我的控制器:

class UsersController <应用控制器定义新@user = User.new@title = "注册"结尾结尾

我的观点是:

注册

<%= form_for(@user) do |f|%><div class="field"><%= f.label :name %><br/><%= f.text_field :name %>

<div class="field"><%= f.label :email %><br/><%= f.text_field :email %>

<div class="field"><%= f.label :password %><br/><%= f.password_field :password %>

<div class="field"><%= f.label :password_confirmation, "Confirmation" %><br/><%= f.password_field :password_confirmation %>

<div class="actions"><%= f.submit "注册" %>

<%结束%>

但是当我在浏览器中加载此视图时,出现异常:

 用户未定义的方法to_key":0x104ca1b60

有人可以帮我解决这个问题吗?

非常感谢!

解决方案

我翻遍了 Rails 3.1 源代码来解决这个问题,我认为这比搜索其他任何地方都容易.早期版本的 Rails 应该是类似的.如果 tl;dr.

,则跳到最后<小时>

当你调用 form_for(@user) 时,你会经历这个:

def form_for(record, options = {}, &proc)#...案例记录当字符串,符号object_name = 记录对象 = 零别的object = record.is_a?(Array) ?record.last : 记录object_name = options[:as] ||ActiveModel::Naming.param_key(object)apply_form_for_options!(记录,选项)结尾

而且由于 @user 既不是字符串也不是对象,您可以通过 else 分支进入 apply_form_for_options!.在 apply_form_for_options! 中,我们看到:

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

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

def record_key_for_dom_id(record)record = record.to_model if record.respond_to?(:to_model)键 = record.to_key钥匙 ?sanitize_dom_id(key.join('_')) : 键结尾

还有您对 to_key 的调用.to_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 值在页面中是唯一的.

<小时>

执行摘要:

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!

解决方案

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:

这篇关于ActiveModel - 视图 - Rails 中的控制器而不是 ActiveRecord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆