gpt4 book ai didi

ruby-on-rails - 无方法错误 : Undefined method 'type' for nil:NilClass in Rails

转载 作者:行者123 更新时间:2023-12-01 05:14:49 25 4
gpt4 key购买 nike

尝试加载我使用 Rails 构建的网站上的其中一个页面时出现 NoMethodError。请参阅下面的完整堆栈跟踪。

如果我在 production.rb 行中将 false 更改为 true

config.cache_classes = false

然后问题就消失了,但是每次对任何文件进行更改时,我都必须重新启动 Rails 服务器。问题似乎与检查我的数据库中列的类型有关,但我不知道如何解决它。

Controller Action :
def manage
@view_data = {}
if (@view_data[:product] = Product.find(params[:id])).nil?
raise ActionController::RoutingError.new('Not Found')
end
@view_data[:backlog] = Task.where("product_id = ? AND status < ?", params[:id], Task::STATUS_COMPLETED)
.page(params[:page]).per(10)
@view_data[:completed] = Task.where(product_id: params[:id], status: Task::STATUS_COMPLETED)
.page(params[:page]).per(10)
end

“任务”表上有四列日期时间类型——我认为这些是导致问题的原因,但我不完全确定。

完整的堆栈跟踪:
NoMethodError - undefined method `type' for nil:NilClass:
activerecord (3.2.13) lib/active_record/attribute_methods/time_zone_conversion.rb:60:in `create_time_zone_conversion_attribute?'
activerecord (3.2.13) lib/active_record/attribute_methods/time_zone_conversion.rb:24:in `attribute_cast_code'
activerecord (3.2.13) lib/active_record/attribute_methods/serialization.rb:80:in `attribute_cast_code'
activerecord (3.2.13) lib/active_record/attribute_methods/read.rb:72:in `define_method_attribute'
activerecord (3.2.13) lib/active_record/attribute_methods/primary_key.rb:29:in `define_method_attribute'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:267:in `block in define_attribute_method'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:168:in `respond_to?'
activesupport (3.2.13) lib/active_support/callbacks.rb:398:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_initialize_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.13) lib/active_record/base.rb:501:in `initialize'
app/controllers/products_controller.rb:14:in `manage'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.13) lib/active_support/callbacks.rb:425:in `_run__115665803178931257__process_action__4080704983955383443__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__4157597128006817714__call__2909158845673021585__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'

最佳答案

就我而言,我有一个具有 76 个属性的模型,其中一个名为“class”,这导致了错误和异常。

所以我做了:

  • rake/bin db:回滚
  • rails d 脚手架 MyModel
  • 在脚手架生成器命令中重命名“class”列
  • 重命名 csv 标题中的“类”
  • rails g 脚手架 MyModel blablaba..
  • rake/bin db:迁移
  • 导入数据
  • rails

  • 和作品:-)

    关于ruby-on-rails - 无方法错误 : Undefined method 'type' for nil:NilClass in Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666366/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com