- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这上面画了一个空白。这是我的问题:
精简版
我的 image#create
操作在调用 image.save
时失败并抛出 Undefined method "name"for nil:NilClass
。 Image 模型包括通过 paperclip
gem 上传文件。
让我失望的部分:
只有当我在提交表单中包含 :multipart => true
时才会出现错误
当我将其删除时,表单可以正常工作,当然图像文件不会上传。有关失败位置的详细信息,请参阅下面的堆栈跟踪。看起来它在尝试为 INSERT 语句引用某些内容时达到了 nil 值,但我不知道是什么。我的 Controller 参数看起来没问题,我通过记录器检查以确保新的 Image
实例在它点击保存调用之前正确构建。
额外细节
我会尽量包括我能想到的所有细节,按重要性排序:
我正在使用 paperclip并尝试通过 S3 存储,尽管当我完全注释掉图像模型中的 has_attached_file
行时仍然会出现此问题。
Image 是 Collection 中的嵌套资源,由 to_param
方法中的 slug 引用。
我掌握了 Multi-Tenancy 系统的基础知识(又名 Basecamp 风格的用户子域,通过涵盖所有组件的中央用户模型实现)。
传递给 Controller 的参数
{"utf8"=>"✓",
"authenticity_token"=>"JYCMky7851j5cW4ChSWUCCL/02iePf6i/QWAgR8q5tE=",
"image"=>{"name"=>"My testerific image 2",
"slug"=>"my-testerific-image-2",
"description"=>"w",
"collection_id"=>"2",
"sort"=>"3",
"picture_file_name"=>#<ActionDispatch::Http::UploadedFile:0x00000101b3f540 @original_filename="David's pic 005.jpeg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"image[picture_file_name]\"; filename=\"David's pic 005.jpeg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/df/mvsv5cs137gbyl8r7h428zp40000gn/T/RackMultipart20110814-64770-duz6wu>>},
"commit"=>"Create Image",
"collection_id"=>"my-new-collection"}
完整堆栈跟踪
activesupport (3.0.9) lib/active_support/whiny_nil.rb:48:in 'method_missing'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:20:in 'block in initialize'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'yield'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'default'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'block in initialize'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'yield'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'default'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'block in initialize'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'yield'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'default'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'block in initialize'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'yield'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'default'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:22:in 'block in initialize'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in 'yield'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in 'default'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in 'accept'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:325:in 'block in dump_ivars'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:323:in 'each'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:323:in 'dump_ivars'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:85:in 'visit_Object'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in 'accept'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in '<<'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych.rb:165:in 'dump'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/core_ext.rb:13:in 'psych_to_yaml'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/quoting.rb:34:in 'quote'
activerecord (3.0.9) lib/active_record/connection_adapters/sqlite3_adapter.rb:45:in 'quote'
arel (2.0.10) lib/arel/visitors/to_sql.rb:327:in 'quote'
arel (2.0.10) lib/arel/visitors/to_sql.rb:70:in 'block in visit_Arel_Nodes_Values'
arel (2.0.10) lib/arel/visitors/to_sql.rb:69:in 'map'
arel (2.0.10) lib/arel/visitors/to_sql.rb:69:in 'visit_Arel_Nodes_Values'
arel (2.0.10) lib/arel/visitors/visitor.rb:15:in 'visit'
arel (2.0.10) lib/arel/visitors/to_sql.rb:59:in 'visit_Arel_Nodes_InsertStatement'
arel (2.0.10) lib/arel/visitors/visitor.rb:15:in 'visit'
arel (2.0.10) lib/arel/visitors/visitor.rb:5:in 'accept'
arel (2.0.10) lib/arel/visitors/to_sql.rb:18:in 'block in accept'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in 'with_connection'
arel (2.0.10) lib/arel/visitors/to_sql.rb:16:in 'accept'
arel (2.0.10) lib/arel/tree_manager.rb:20:in 'to_sql'
arel (2.0.10) lib/arel/select_manager.rb:217:in 'insert'
activerecord (3.0.9) lib/active_record/relation.rb:14:in 'insert'
activerecord (3.0.9) lib/active_record/persistence.rb:274:in 'create'
activerecord (3.0.9) lib/active_record/timestamp.rb:47:in 'create'
activerecord (3.0.9) lib/active_record/callbacks.rb:277:in 'block in create'
activesupport (3.0.9) lib/active_support/callbacks.rb:414:in '_run_create_callbacks'
activerecord (3.0.9) lib/active_record/callbacks.rb:277:in 'create'
activerecord (3.0.9) lib/active_record/persistence.rb:250:in 'create_or_update'
activerecord (3.0.9) lib/active_record/callbacks.rb:273:in 'block in create_or_update'
activesupport (3.0.9) lib/active_support/callbacks.rb:429:in '_run_save_callbacks'
activerecord (3.0.9) lib/active_record/callbacks.rb:273:in 'create_or_update'
activerecord (3.0.9) lib/active_record/persistence.rb:40:in 'save'
activerecord (3.0.9) lib/active_record/validations.rb:43:in 'save'
activerecord (3.0.9) lib/active_record/attribute_methods/dirty.rb:21:in 'save'
activerecord (3.0.9) lib/active_record/transactions.rb:240:in 'block (2 levels) in save'
activerecord (3.0.9) lib/active_record/transactions.rb:292:in 'block in with_transaction_returning_status'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in 'transaction'
activerecord (3.0.9) lib/active_record/transactions.rb:207:in 'transaction'
activerecord (3.0.9) lib/active_record/transactions.rb:290:in 'with_transaction_returning_status'
activerecord (3.0.9) lib/active_record/transactions.rb:240:in 'block in save'
activerecord (3.0.9) lib/active_record/transactions.rb:251:in 'rollback_active_record_state!'
activerecord (3.0.9) lib/active_record/transactions.rb:239:in 'save'
app/controllers/images_controller.rb:42:in 'block in create'
actionpack (3.0.9) lib/action_controller/metal/mime_responds.rb:264:in 'call'
actionpack (3.0.9) lib/action_controller/metal/mime_responds.rb:264:in 'retrieve_response_from_mimes'
actionpack (3.0.9) lib/action_controller/metal/mime_responds.rb:191:in 'respond_to'
app/controllers/images_controller.rb:40:in 'create'
actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in 'send_action'
actionpack (3.0.9) lib/abstract_controller/base.rb:150:in 'process_action'
actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:in 'process_action'
actionpack (3.0.9) lib/abstract_controller/callbacks.rb:18:in 'block in process_action'
activesupport (3.0.9) lib/active_support/callbacks.rb:451:in '_run__867212960703161301__process_action__2360218484576085785__callbacks'
activesupport (3.0.9) lib/active_support/callbacks.rb:410:in '_run_process_action_callbacks'
activesupport (3.0.9) lib/active_support/callbacks.rb:94:in 'run_callbacks'
actionpack (3.0.9) lib/abstract_controller/callbacks.rb:17:in 'process_action'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:30:in 'block in process_action'
activesupport (3.0.9) lib/active_support/notifications.rb:52:in 'block in instrument'
activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in 'instrument'
activesupport (3.0.9) lib/active_support/notifications.rb:52:in 'instrument'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:in 'process_action'
actionpack (3.0.9) lib/action_controller/metal/rescue.rb:17:in 'process_action'
actionpack (3.0.9) lib/abstract_controller/base.rb:119:in 'process'
actionpack (3.0.9) lib/abstract_controller/rendering.rb:41:in 'process'
actionpack (3.0.9) lib/action_controller/metal.rb:138:in 'dispatch'
actionpack (3.0.9) lib/action_controller/metal/rack_delegation.rb:14:in 'dispatch'
actionpack (3.0.9) lib/action_controller/metal.rb:178:in 'block in action'
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in 'call'
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in 'dispatch'
actionpack (3.0.9) 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.9) lib/action_dispatch/routing/route_set.rb:493:in 'call'
warden (1.0.5) lib/warden/manager.rb:35:in 'block in call'
warden (1.0.5) lib/warden/manager.rb:34:in 'catch'
warden (1.0.5) lib/warden/manager.rb:34:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/head.rb:14:in 'call'
rack (1.2.3) lib/rack/methodoverride.rb:24:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/params_parser.rb:21:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/session/abstract_store.rb:149:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in 'call'
activerecord (3.0.9) lib/active_record/query_cache.rb:32:in 'block in call'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in 'cache'
activerecord (3.0.9) lib/active_record/query_cache.rb:12:in 'cache'
activerecord (3.0.9) lib/active_record/query_cache.rb:31:in 'call'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in 'block in call'
activesupport (3.0.9) lib/active_support/callbacks.rb:416:in '_run_call_callbacks'
actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:44:in 'call'
rack (1.2.3) lib/rack/sendfile.rb:107:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/show_exceptions.rb:47:in 'call'
railties (3.0.9) lib/rails/rack/logger.rb:13:in 'call'
rack (1.2.3) lib/rack/runtime.rb:17:in 'call'
activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in 'call'
rack (1.2.3) lib/rack/lock.rb:11:in 'block in call'
<internal:prelude>:10:in 'synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in 'call'
actionpack (3.0.9) lib/action_dispatch/middleware/static.rb:30:in 'call'
railties (3.0.9) lib/rails/application.rb:168:in 'call'
railties (3.0.9) lib/rails/application.rb:77:in 'method_missing'
railties (3.0.9) lib/rails/rack/log_tailer.rb:14:in 'call'
rack (1.2.3) lib/rack/content_length.rb:13:in 'call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in 'service'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in 'service'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in 'run'
/Users/daw/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in 'block in start_thread'
根据要求,型号代码:
class Image < ActiveRecord::Base
belongs_to :collection
belongs_to :user
before_save :create_slug
has_attached_file :picture,
#:styles => {
# :thumb => '100x100>',
# :full => '800x800>'
#},
:storage => :s3,
:s3_credentials => Rails.root.join( 'config', 's3.yml' ),
:path => ":id/:style/:filename"
def to_param
self.name.parameterize
end
private
def create_slug
self.slug = self.to_param
end
end
我比较了使用 :multipart => true
和没有构建的 Image
实例,确认唯一的区别是文件上传字段,picture_file_name
。如果没有 :multipart => true
,它是一个带有文件名的字符串(不足为奇)。有了它,它就是 ActionDispatch::Http::UploadedFile
的一个实例。鉴于堆栈跟踪显示它在 to_yaml
调用期间失败,也许问题在于 UploadedFile
实例转换为 YAML?
最佳答案
好吧,我终于放下心来,通过记录器输出追踪了 ActiveRecord 源代码。结果,正如我所怀疑的那样,它未能保存 UploadedFile
,因为它无法将其转换为 YAML。
但是,这是因为我犯了一个愚蠢的错误。在我的提交表单中,我有:
<%= f.file_field :picture_file_name %>
应该是什么时候:
<%= f.file_field :picture %>
paperclip
gem 理解 :picture
是上传的文件,并使用后台魔法将文件名保存到正确的字段。我只是低估了我猜的易用性!
通常,我会删除这种微不足道的问题,但鉴于我犯了这个错误,将来其他人可能会这样做,而且几乎没有什么可以指出我的解决方案。因此,我会继续关注它,以帮助将来在谷歌上搜索同样密集的开发人员!
关于ruby-on-rails - 添加 :multipart => true throws Undefined Method "name" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7055074/
sanitize 是什么意思在 Rails 中是什么意思? 我正在阅读 CanCanCan 的文档.它说: When using strong_parameters or Rails 4+, you
在过去的几个月里,我感觉自己对 Ruby on Rails (RoR) 开发的了解达到了极限。我为大/小客户和 friend /爱好项目开发了大大小小的应用程序。我知道如何开发这些应用程序,但开始感觉
我昨天参加了一个关于扩展 Rails 的聚会,其中一个主题是 Hexagonal Rails。然而,我只做了一年的 Rails,对 MVC 结构非常满意(也许太舒服了),所以我不太了解适配器和消息队列
我使用多个 Rails 应用程序,一些在 Rails 3.2/Ruby 2.0 上,一些在 Rails 2.3/Ruby 1.8.7 上。 他们的共同点是,随着他们的成长和添加更多的依赖项/ gem
这个问题在这里已经有了答案: Using Rails-UJS in JS modules (Rails 6 with webpacker) (5 个答案) 关闭 3 年前。 我正在尝试使用 UJS
我正在开发一个当前使用 Rails 1.2 的 Rails 应用程序,所以我现在离最新的稳定版本(Rails 2.3)还有很长的路要走。 我应该如何进行迁移到更新版本的 Rails 的过程? 我应该一
尝试按照 Ryan Bates Backbone.js 教程构建抽奖应用程序,但我已经遇到了第一段代码的问题。在 application.js 的 init 函数中,他初始化了 Raffler 路由的
我正在使用 Rails 3.2 并且我有一个数据库表,我想在其中找到符合以下条件的所有行: a = true and b = true and ( 0 true, :b =>
我有一个用户类和一个联系人,其中联系人是用户的子类。这两个类都存储在用户表中。 我的联系人可能有也可能没有电子邮件地址,而我的用户需要一个电子邮件地址(我的用户模型定义中有 validates_pre
我正在编写一个教程,我在其中演示了一些 rails 命令。在我的机器上 rails和 script/rails两者都同样有效。有“首选”形式吗?两者中哪一个更普遍? 最佳答案 当您运行 rails 时
我正在寻找有关通过我的应用程序前进的最佳方式的建议,这是我首次开始集成Elasticsearch。我是一名初学者,但是热衷于深入研究,以便原谅任何明显的错误! 我遵循了http://www.sitep
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我有 Rails 4,这是我的默认版本(我仍然希望它是)。但我不想在我的电脑上添加 rails 3.2。在以下命令中:gem install rails -v 3.2.16 我有这个警告: railt
您好,我想使用 Sheevaplug 构建一个“Rails Brick”来自 Marvell(操作系统是开箱即用的 Ubuntu,但您可以在其上安装其他发行版)。它将成为家庭服务器和静音、低成本(99
我需要能够从 Rails 控制台发送我的 Rails 应用程序的 Postgres 数据库中所有未接受的邀请。 (我有一个名为 Invitations 的表,其中包含一个名为 accepted 的 b
validate :cannot_modify_if_locked, on: :update def cannot_modify_if_locked if self.locked erro
我正在学习教程(学习 Rails 播客),需要更改以下路由语法,以便它与 Rails 3.0 兼容。谁能帮忙? map.view_page ':name', :controller => 'viewe
我是一名优秀的程序员,十分优秀!