Rails无法在第一次尝试时自动加载常量

编程入门 行业动态 更新时间:2024-10-28 04:30:16
本文介绍了Rails无法在第一次尝试时自动加载常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的Rails 4应用中,我有一个基本模型,我们称它为Badge,它位于/app/models/badge.rb中.

然后,我有大量从徽章继承的特定徽章,比如说GoldBadge和SilverBadge(实际上还有更多).所有这些文件都位于/app/model/badge/<name>_badge.rb中.例如,/app/model/badge/gold_badge.rb.

GoldBadge的类声明看起来像这样

class GoldBadge < Badge ...

但是,Rails(或我不太了解的Ruby)自动加载器在仅在初次尝试时无法找到这些文件.我的意思是,例如,当我第一次访问需要使用GoldBadge的页面时,出现以下错误:

Unable to autoload constant Badge::GoldBadge, expected /path_to_my_rails_app/app/models/badge/gold_badge.rb to define it

这很奇怪,因为该类是在该文件中定义的.但是,甚至更陌生的是,如果我随后刷新页面,一切都会正常-找到该类!

在我引用这些徽章的多种不同类型的页面上,我必须为每种不同类型刷新一次页面,然后才能起作用.例如,如果我有一个使用GoldBadge,SilverBadge和BronzeBadge的页面,那么我将刷新该页面3次,对于每种徽章类型,该错误都会显示一次,直到最终生效.

这是在开发模式下发生的.根据我的阅读,这可能会在生产中消失,但是在开发模式中仍然很烦人.有什么方法可以修复它(最好没有缓存类)?

解决方案

如果将其放在命名空间下(例如app/models/badge文件夹),则必须定义类,例如:

class Badge::GoldBadge < Badge #... end

以自动加载它并能够像Badge::GoldBadge那样访问它. 如果要仅通过调用GoldBadge进行访问,则将其移至app/models目录.

In my Rails 4 app, I have a base model, let's call it Badge, which lives in /app/models/badge.rb.

Then, I have a large number of specific badges that inherit from Badge, let's say GoldBadge and SilverBadge (and many more, in reality). All of these files live in /app/model/badge/<name>_badge.rb. For example, /app/model/badge/gold_badge.rb.

The class declaration for GoldBadge would look like this

class GoldBadge < Badge ...

However, the Rails (or Ruby, I don't really know) auto-loader, has trouble locating these files only on the first attempt. What I mean by that is when I first access a page that needs to use GoldBadge, for example, I get the following error:

Unable to autoload constant Badge::GoldBadge, expected /path_to_my_rails_app/app/models/badge/gold_badge.rb to define it

Which is weird, because the class is defined in that file. However, even stranger, is that if I then refresh the page, everything works - the class is found!

On a page where I reference multiple different types of these badges, I have to refresh the page once for each different type, before it will work. For example, if I had a page that used GoldBadge, SilverBadge, and BronzeBadge, I would refresh the page 3 times, with that error being shown once for each badge type, before it would finally work.

This happens in development mode. From what I've read, this might go away in production, but it's still very annoying in development mode. Any way to fix it (preferably without caching classes)?

解决方案

If you put it under namespace, like app/models/badge folder, you have do define your class like:

class Badge::GoldBadge < Badge #... end

to autoload it and to be able to access it in the way like Badge::GoldBadge. If you want to access it by just calling GoldBadge, then move it to app/models directory.

更多推荐

Rails无法在第一次尝试时自动加载常量

本文发布于:2023-09-10 15:23:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466861.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   自动加载   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!