是否可以为简单类创建表单

时间:2011-08-25 09:39:32

标签: ruby-on-rails

我只想拥有编辑类属性的能力,而不是ActiveRecord类的继承者。

这是我的班级/app/model/entry.rb:

class Entry
  attr_accessor :title, :body
end

这是一个控制器:

class DiaryController < ApplicationController
  def new
    @entry = Entry.new
  end
end

以下是一个观点:

<%= form_for(@entry) do |f| %>
<%= f.text_field :title %> <br />
<%= f.text_area :body %> <br />
<% end %>

我在打开表单时收到错误消息:

  

条目的未定义方法`model_name':Class

这是完整的堆栈:

Extracted source (around line #1):

1: <%= form_for(@entry) do |f| %>
2: <%= f.text_field :title %> <br />
3: <%= f.text_area :body %> <br />
4: <% end %>


Full trace:

activemodel (3.0.6) lib/active_model/naming.rb:95:in `model_name_from_record_or_class'
activemodel (3.0.6) lib/active_model/naming.rb:82:in `singular'
actionpack (3.0.6) lib/action_view/helpers/form_helper.rb:312:in `form_for'
app/views/diary/new.html.erb:1:in `_app_views_diary_new_html_erb___902027577_26033568_187052521'
actionpack (3.0.6) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.6) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.6) lib/action_view/template.rb:127:in `render'
actionpack (3.0.6) lib/action_view/render/rendering.rb:59:in `block in _render_template'
activesupport (3.0.6) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.6) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.6) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.6) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.6) lib/abstract_controller/rendering.rb:115:in `_render_template'
actionpack (3.0.6) lib/abstract_controller/rendering.rb:109:in `render_to_body'
actionpack (3.0.6) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.6) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.6) lib/abstract_controller/rendering.rb:102:in `render_to_string'
actionpack (3.0.6) lib/abstract_controller/rendering.rb:93:in `render'
actionpack (3.0.6) lib/action_controller/metal/rendering.rb:17:in `render'
actionpack (3.0.6) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.0.6) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
c:/Ruby192/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
activesupport (3.0.6) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.0.6) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.0.6) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.0.6) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
actionpack (3.0.6) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.0.6) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.0.6) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.0.6) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.6) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.6) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.6) lib/active_support/callbacks.rb:436:in `_run__776311234__process_action__123531476__callbacks'
activesupport (3.0.6) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.6) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.6) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.6) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.6) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.6) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.6) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.6) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.6) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.6) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.6) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.6) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.6) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.6) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.6) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.6) lib/action_dispatch/routing/route_set.rb:493:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.2) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.6) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.6) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.6) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.6) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.6) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.6) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.2) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.6) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.2) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.2) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.2) lib/rack/lock.rb:11:in `call'
actionpack (3.0.6) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.6) lib/rails/application.rb:168:in `call'
railties (3.0.6) lib/rails/application.rb:77:in `method_missing'
railties (3.0.6) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.2) lib/rack/content_length.rb:13:in `call'
rack (1.2.2) lib/rack/handler/webrick.rb:52:in `service'
c:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
c:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
c:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

是否可以使用form_for - 代码编辑类属性?

5 个答案:

答案 0 :(得分:5)

jdc回答了这个问题,但未显示您请求的实际实施。正如jdc所说,你需要包含一些ActiveModel模块,这将为你的ruby类提供你需要的功能。

看起来你需要ActiveModel :: Naming和ActiveModel :: AttributeMethods。

class Entry
  extend ActiveModel::Naming
  include ActiveModel::AttributeMethods
end

第一行将为您提供model_name,第二行允许您的模型与表单助手一起使用。您还可以使用其他行:查看在jdc的答案中链接的博客或Rails API

详细信息的更多链接:

http://www.rubyinside.com/rails-3-0s-activemodel-how-to-give-ruby-classes-some-activerecord-magic-2937.html

http://railscasts.com/episodes/219-active-model

答案 1 :(得分:2)

此博客文章可以帮助您入门:

http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/

您可以包含一些ActiveRecord用于验证和序列化等模块的模块。然后,如果你想要一个不会进入数据库的“保存”方法,只需创建一个。我这样做是为了一个联系模式。它不是AR,只是一个普通的旧ruby对象,它引入了一些ActiveModel的东西进行验证,提供了一些必需的方法,然后save方法只会触发一封电子邮件。我认为甚至还有一个mixin可以用来处理初始化的东西,我正在做的是允许在创建时进行质量分配,但我没有密切关注它。这个对象看起来像是前端的AR对象,所以基本的帮助器都可以工作。

class Contact
  include ActiveModel::Validations

  attr_accessor :id, :name, :email, :subject, :body

  validates :name, :presence => true
  validates :email, :presence => true
  validates :subject, :presence => true
  validates :body, :presence => true

  def initialize(attributes = {})
    attributes.each do |key, value|
      self.send("#{key}=", value)
    end
    @attributes = attributes
  end

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  def to_key
  end

  def save
    if self.valid?
      ContactNotifier.website_contact(self).deliver
      return true
    end
    false
  end
end

答案 2 :(得分:0)

看起来你得到的错误来自form_helper.rb中的以下行:

object_name = options[:as] || ActiveModel::Naming.param_key(object)

如果你硬编码:as =&gt; “进入”,你会更进一步。或者您可以扩展类以响应方法model_name。以下是调用model_name的ActiveModel :: Naming中的代码:

 def self.model_name_from_record_or_class(record_or_class)
        (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
      end

我可以继续接听电话,但我建议您自己关注。最终,您将找到一个调用,该调用返回表单生成器在form_for中使用的属性和值的散列。

只需查看rails源代码,查看代码引发错误的位置,并扩展您的类以响应正确的调用。继续这样做,你的模型会很好地回应。您不需要成为活动记录模型就可以像活动记录模型那样查看,行走和发声。

答案 3 :(得分:0)

我发现这个RailsCasts Episode#193对你要做的事情非常有帮助。

答案 4 :(得分:-1)

首先,您的条目模型文件应如下所示:

class Entry < ActiveRecord::Base