English 中文(简体)
积极模型——观点——铁路主计长而不是主动记录?
原标题:ActiveModel - View - Controller in Rails instead of ActiveRecord?

我不想让我的模型与数据库有任何关系,而是试图使用主动的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

这里是我的控制者:

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

没有人会帮助我这样做?

很多事事事事事事事事事事事事前会!

最佳回答

我在铁路3.1来源周围扎根,以划出这一点,我觉得,比在其他地方寻找更容易。 铁路的较早版本应当类似。 Jump to the end if 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

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
)

注意这部法典,既包括问题的根源,也包括解决办法。 <代码>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。 <代码>至_key http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey.html>ActiveRecord:AttributeMethods:PrimaryKey 由于你不再使用主动记录,你没有<条码>至_key方法。 如果在你的模式中有一些事情像一个主要钥匙一样,那么你可以界定自己的<条码>至_key<>,然后留任。

但是,如果我们回到<代码>应用_form_for_options!。 我们看到另一种解决办法:

as = options[:as]

因此,您可以提供<条码>:作为的选项,<条码>格式>,以便用手方式生成电传识别器:

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

您必须确保<代码>:数值在网页上是独一无二的。


www.un.org/Depts/DGACM/index_spanish.htm 内容提要

  • 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.
问题回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签