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

2024-04-20

我只是希望能够编辑不是 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 %>

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

Entry:Class 的未定义方法“model_name”

这是完整的堆栈:

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-tag?


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

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

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

第一行会给你model_name第二行允许您的模型与表单助手一起运行。您还可以使用其他行:查看 jdc 答案中链接的博客或轨道API http://api.rubyonrails.org/.

更多包含详细信息的链接:

http://www.rubyinside.com/rails-3-0s-activemodel-how-to-give-ruby-classes-some-activerecord-magic-2937.html 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 http://railscasts.com/episodes/219-active-model

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以为简单的类创建表单 的相关文章

  • 如何覆盖 Ruby Ranges 的 .. 和 ... 运算符以接受 Float::INFINITY?

    我想覆盖 and Ruby 中的运算符Range 原因是 我正在处理数据库中的无限日期范围 如果你拉一个infinty从 Postgres 中取出日期时间 你会得到一个Float INFINITY在红宝石中 问题是 我无法使用Float I
  • Mongoid - 同一外域的两个域的逆

    我试图让以下 Mongoid 关系发挥作用 但每个团队的游戏字段都是一个空数组 这不是一个有效的关系模型吗 我是否需要分开比赛 即主场比赛和客场比赛 class Team include Mongoid Document has many
  • 如何阻止与 RSpec 和 Capybara 的外部连接?

    在我的 Rails 项目中 我想编写非理想条件的测试 例如缺乏互联网连接或超时 例如 我正在使用 gem 来联系 API 并且希望确保在我的应用程序和外部 API 之间存在连接问题时能够正确处理错误 我已经可以通过用录像机制作固定装置并从
  • MONGODB [DEBUG] 游标的cursor.refresh() 7078636577051629992

    更新大型 json 列表时 出现以下错误 2012 04 01T09 34 00 00 00 app run 1 MONGODB DEBUG cursor refresh for cursor 7078636577051629992 201
  • 渲染:带参数的动作

    我有一个有 2 种方法的类 第一个方法由视图使用一些 GET 参数 params page 调用 我想保存这些参数并通过渲染操作将它们发送到我的第二个方法 class exemple def first sql save of params
  • 如何使 Rails 3 资源预编译速度更快?

    我有一个正在运行的 Rails 3 2 1 应用程序 我正在通过 Capistrano 进行部署deploy assets它运行deploy assets precompile task 一切工作都很好 除了编译本身非常慢 我没有那么多 C
  • Mongoid 中的 find_or_create_by 线程安全吗?

    我有一个使用 Mongoid 的 find or create by 方法的网络应用程序 poll Poll find or create by fields 在投入生产之前 我尝试运行故障场景 我发现多个用户可以尝试使用此方法访问此资源
  • 如何在 Rails 3.2.1 版本中注释 Rails 模型

    我正在尝试遵循一些在线教程来在 Rails 中注释我的模型 然而 似乎所有教程都在谈论过时的注释版本或不正确的安装 这真是一团糟 到目前为止我已经尝试过以下方法 1 在 Gemfile 中添加此内容 gem annotate 2 4 0 2
  • 当一组工作人员完成时如何执行 Sidekiq 回调

    假设我有一个 Sidekiq 任务将产品处理到我的数据库 每个产品都按商店分组 因此我的代码的一个过于简化的示例将是这样的 stores each do store store products each do product Produc
  • 忽略 git 中的本地配置文件

    Rails 应用程序中有一些本地文件 属于我们存储库的一部分 我希望 git 忽略它们 基本上 我希望 git 忽略我对 config environments 目录和 config application rb 文件中的任何内容所做的所有
  • 获取设备中的当前用户ID

    如何使用设备获取控制器中的当前用户 ID 在我的控制器中我有这样的东西 def index me current user c User find me sheets c time sheets end 我收到一条错误消息 无法找到没有 I
  • 捆绑安装无法从 https://rubygems.org/ 获取规格

    我正在尝试遵循 Hartl Rails 教程 但在使用捆绑器 gem 时遇到了问题 使用命令 bundle install 或 bundle update 时 我得到以下输出 从中获取源索引https rubygems org https
  • 预期的 ProductField,出现数组问题

    我有一个 Rails 4 应用程序 它有一个如下所示的 params 块 def store params params require store permit name description user id products attr
  • 使用 Ajax 在输入时提交 Textarea,然后渲染部分内容而不刷新整个页面

    目前我正在尝试通过 JS 和 Ajax 在没有提交按钮的情况下发表评论 并且它有效 问题是当帖子提交时 页面重新加载到 post id comment页面 我希望它呈现部分内容 而不是刷新整个页面或将我带到不同的页面 我对 JS 不熟悉 任
  • 如何在 Rails 4 中使用 params.require

    我有一个像这样的私有方法 用于有四个字段的注册表单 firstname email password and confirm password 我不知道如何检查password confirmation def user params pa
  • Rails 3 在 Ruby 1.9.2 上初始化非常慢

    我使用 RVM 来管理环境 安装了 Ruby 1 9 2 p136 我认为是最新版本 和 Rails 3 创建了 gemset 并运行捆绑器 到目前为止一切正常 但 Rails 在运行命令 即生成 销毁 rake 等 时初始化速度非常慢 完
  • 用户未定义的方法 attr_accessible 错误

    我正在尝试创建某种登录 我创建了一个用户脚手架并将此代码放在我的 user rb 中 class User lt ActiveRecord Base attr accessible name password digest password
  • 如何加载页面特定的rails 4 js文件?

    我正在阅读资产管道的 Rails 指南文档 它指出 CoffeeScript 页面特定生成的文件 如果清单上有 require tree 指令 则默认情况下可供用户使用 这对我不起作用我必须包括这个 在特定控制器上 我缺少什么 资产管道会将
  • 如何为 ApplicationController 中 after_action 过滤器中的所有操作渲染 json?

    是否可以在 Rails ApplicationController 中创建一个 after filter 方法 该方法在每个操作上运行并呈现为 JSON 我正在构建一个 API 并且希望将控制器中的每个操作的输出呈现为 JSON 客户控制器
  • 将rails_admin 与rails_api 结合使用

    我最初将此发布为Rails api GitHub 上的问题 https github com sferik rails admin issues 2617 但由于不活跃 我现在将其发布在这里 我正在尝试使用rails admin使用 Rai

随机推荐

  • 工具栏不适用于使用 appcompat v7 的棒棒糖之前的设备

    我正在使用材质 appcompat v7 为工具栏和菜单抽屉编写简单的代码 一切在 Nexus 5 上与棒棒糖完美配合 但在棒棒糖之前的版本 4 1 或 4 4 中设备会崩溃 问题在于定义风格 如果有人能告诉我问题出在哪里 我就把我的代码贴
  • 带有 SimpleCursorAdapter 的 NullPointerException

    尝试使用自定义适配器设置 gridview 我获取光标并在 ASyncTask 中设置适配器 这是我的全部代码 private class getAllData extends AsyncTask
  • 无法识别启动活动:未找到默认活动

    我是android新手 遇到了一个问题 控制台显示 无法识别启动活动 未找到默认活动 我已经添加了
  • FREEMARKER:避免转义 HTML 字符

    freemarker 输出有问题 assign optionsHTML list data as item assign optionsHTML optionsHTML
  • Cassandra 3.11.3和cqlsh不支持python 3.6和3.7? [复制]

    这个问题在这里已经有答案了 我已经安装了Python 3 6 或 3 7 with 卡桑德拉 3 11 3 但不支持cqlsh 它只支持Python 2 7版本 这是错误消息 apache cassandra 3 11 3 bin cqls
  • Typescript - 无法调用类型缺少签名的表达式

    我对打字稿很陌生 当我尝试在打字稿中像这样调用这个 javascript 时 window location http localhost 1773 Repository NetworkPlan ExportPng 我收到这样的编译器错误消
  • 如何使用 .htaccess 提供 gzip 压缩字体? (没有 mod gzip 或 deflate)

    以下是我按随机顺序尝试过的内容列表 AddHandler application x httpd php otf AddType default mimetype auto prepend file otf php zlib output
  • 对 Java 集合进行排序和分组

    我有一个有名称和分数的对象 我想对此类对象的集合进行排序 以便它们按名称分组并按每组中的最大分数排序 并且在组内也按降序分数排序 让我展示一下我想要实现的目标 假设我有这些对象 名称 分数 a 3 a 9 b 7 b 10 c 8 c 3
  • Spark 使用前一行的值向数据帧添加新列

    我想知道如何在 Spark Pyspark 中实现以下目标 初始数据框 id num 4 9 0 3 7 0 2 3 0 1 5 0 结果数据框 id num new Col 4 9 0 7 0 3 7 0 3 0 2 3 0 5 0 我通
  • 可加载组件:异步加载组件失败

    我创建了模块 A 它是我的 React 应用程序的组件库 我计划在模块 B 上使用它 这是我实际的 React 应用程序 我有一个 index js 通过以下方式使用可加载组件从模块 A 导出我的组件 import loadable fro
  • ComboBoxModel 事件不起作用

    我似乎不理解事件等概念 在阅读了有关如何实现侦听器等内容的一段时间后 我发现 Java 教程说我应该扩展AbstractListModel触发数据事件 由于某种原因它仍然不起作用 我做错了什么吗 以及预计会出现什么样的代码addListDa
  • C++中如何处理bad_alloc?

    有一种方法叫做foo有时会返回以下错误 terminate called after throwing an instance of std bad alloc what std bad alloc Abort 有没有一种方法可以让我使用t
  • 将 mogenerator 集成到 Xcode 4 中

    在我的应用程序中 我使用核心数据来实现持久数据保存 因为我已经看到 mogenerator 提供了一种很好的方法来创建和维护NSManagedObject子类 也具有附加功能 我正在寻找一些在 Xcode 4 中集成 monogenerat
  • 如何将 CSS 应用到 Mac Chrome 选择框?

    无论我做什么 使用 Mac OSX 10 9 2 和 Chrome 版本 33 0 1750 152 padding background color 没有任何效果 我真的只是想申请一个padding top and padding bot
  • 直到用户输入匹配变量为止

    好的 所以我正在尝试创建一个用户名 密码登录脚本 可能不是我仍在研究的最安全的想法 我的脚本将加载变量以与这样的文件进行比较 现在我只是在研究密码部分 path to variables conf 该文件将包含一个名为的变量 PASS SO
  • C# 睡眠检测 PowerModeChanged

    我希望我的程序在计算机进入睡眠状态时写入控制台 但我无法这样做 我认为我设置错误 这是我所拥有的 编辑 新的 cs 文件 using LightFX using System using System Threading using Sys
  • 如何让 Pyglet 为 Python 3 工作?

    我很高兴能使用 Pyglet 因为它的所有功能 但是我在 Python 3 上安装 Pyglet 的最新开发版本时遇到问题 我知道人们已经问过这个问题 但没有一个回答对我有任何帮助 更新 我的意思是 我无法为 Python 3 安装 Pyg
  • 循环动态展开子网格 - JQGrid

    我有一个带有使用 subGridRowExpanded 的子网格的网格 我想动态扩展网格的一些行 所以我写了以下内容 在第一个网格的 GridComplete 事件中 ids 是我的网格的行 id 数组 for int i 0 i lt i
  • 截断轨道后无法使用原始数据

    问题是如果我使用然后我尝试打印它 我仍然看到所有 html 标签 truncate默认情况下转义字符串 但您可以使用将其关闭 escape option description truncate post content separator
  • 是否可以为简单的类创建表单

    我只是希望能够编辑不是 ActiveRecord 类的后继类的类属性 这是我的类 app model entry rb class Entry attr accessor title body end 这是一个控制器 class Diary