gpt4 book ai didi

ruby-on-rails - 使用 `id' gem 时出现 Rails NoMethodError(未定义方法 'protected attributes' ...)

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:17 25 4
gpt4 key购买 nike

我在 Rails 中使用“protected_attributes”gem,它将显式 attr_accessible 带回到 Rails 4(我需要与另一个 Gem 兼容,长话短说)。

在编写代码的过程中,我试图访问一个未命名为可访问的属性。我收到以下错误:

NoMethodError (undefined method `id' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x00000104c51c00>):

意识到我的错误,我将 :id 添加到我的 attr_accessible 列表中。但是,即使在重新启动 rails 服务器之后,每次运行代码时我仍然会收到完全相同的错误。这以前发生在我身上,我所做的是 rake db:reset,无论出于何种原因,它解决了我的问题(但在此过程中破坏了我的数据库)。我不想再次清除我的数据库...有更简单的方法来解决这个问题吗?一些缓存我没有清除什么的?谢谢。

编辑:对于那些感兴趣的人,这是完整的跟踪:

activerecord (4.0.0) lib/active_record/relation/delegation.rb:121:in `method_missing&#39;
activerecord (4.0.0) lib/active_record/relation/delegation.rb:68:in `method_missing&#39;
app/controllers/api/v1/superusers.rb:17:in `block (2 levels) in &lt;class:Users&gt;&#39;
grape (0.7.0) lib/grape/endpoint.rb:31:in `call&#39;
grape (0.7.0) lib/grape/endpoint.rb:31:in `block in generate_api_method&#39;
grape (0.7.0) lib/grape/endpoint.rb:407:in `call&#39;
grape (0.7.0) lib/grape/endpoint.rb:407:in `run&#39;
grape (0.7.0) lib/grape/endpoint.rb:156:in `block in call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call&#39;
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call&#39;
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call&#39;
grape (0.7.0) lib/grape/middleware/error.rb:26:in `block in call!&#39;
grape (0.7.0) lib/grape/middleware/error.rb:25:in `catch&#39;
grape (0.7.0) lib/grape/middleware/error.rb:25:in `call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call&#39;
rack (1.5.2) lib/rack/head.rb:11:in `call&#39;
rack (1.5.2) lib/rack/builder.rb:138:in `call&#39;
grape (0.7.0) lib/grape/endpoint.rb:157:in `call!&#39;
grape (0.7.0) lib/grape/endpoint.rb:145:in `call&#39;
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call&#39;
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize&#39;
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each&#39;
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize&#39;
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call&#39;
grape (0.7.0) lib/grape/api.rb:537:in `call&#39;
grape (0.7.0) lib/grape/api.rb:44:in `call!&#39;
grape (0.7.0) lib/grape/api.rb:40:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call&#39;
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each&#39;
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call&#39;
rack (1.5.2) lib/rack/etag.rb:23:in `call&#39;
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call&#39;
rack (1.5.2) lib/rack/head.rb:11:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call&#39;
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call&#39;
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call&#39;
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call&#39;
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__3113182128831505771__call__callbacks&#39;
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call&#39;
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app&#39;
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call&#39;
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged&#39;
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged&#39;
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged&#39;
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call&#39;
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call&#39;
rack (1.5.2) lib/rack/runtime.rb:17:in `call&#39;
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call&#39;
railties (4.0.0) lib/rails/engine.rb:511:in `call&#39;
railties (4.0.0) lib/rails/application.rb:97:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
rack (1.5.2) lib/rack/content_length.rb:14:in `call&#39;
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service&#39;
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service&#39;
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run&#39;
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread&#39;

最佳答案

错误发生是因为您尝试在 ActiveRecord::Relation 元素上调用 :id 方法。

尝试做:

@your_element.first.id

关于ruby-on-rails - 使用 `id' gem 时出现 Rails NoMethodError(未定义方法 'protected attributes' ...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24247068/

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