Rails 徽章/成就系统动态主列表

发布于 2024-10-11 08:39:03 字数 712 浏览 2 评论 0原文

所以我使用以下方法: 如何在 RoR 中实现成就系统 在我的应用程序上实现徽章/成就系统。

到目前为止,这效果很好,但我希望有一个网站上当前存在的所有徽章的“主列表”。由于这个数量不断增加,我希望这个列表能够动态地自行填充,而不是我必须手动更新它。

这听起来很简单,但是由于成就系统的工作方式(各种类型的徽章都是 Achievement.rb 模型的子类......数据库中有许多相同徽章的实例)我不确定如何能够确定成就模型的所有子类。

此外,我希望每个徽章都有自己的显示页面(示例网址:http://www. mysite.com/achievements/badge1

因此,在这个主列表中,徽章图像将如下所示:

<%= link_to "#{image_tag @achievement.photo}", achievement_path(@achievement) %>

但是,我再次不知道如何迭代成就模型的所有子类。

这有道理吗?我该怎么做呢?

谢谢,

So I am using the method in: How to implement an achievement system in RoR to implement a badges/achievement system on my app.

This has worked great thus far, but I am wanting to have a "master list" of all the badges that currently exist on the site. As this is continually increasing, I'd like it if this list could dynamically populate itself, instead of me having to update it by hand.

This sounds pretty easy, but because of how the achievements system works (the various types of badges are all subclasses of the Achievement.rb model...there are many instances of the same badges in the db) I am not sure of how to be able to determine all the subclasses of the Achievements model.

Additionally, I would like for each badge to have its own show page (example url: http://www.mysite.com/achievements/badge1)

So within this master list the badge images will look something like this:

<%= link_to "#{image_tag @achievement.photo}", achievement_path(@achievement) %>

However, yet again, I have no idea how to iterate through all the subclasses of the Achievement model.

Does this make sense? How should I go about doing this?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-10-18 08:39:03

要迭代子类,您应该能够执行以下操作:

#Get the subclasses as class objects
Achievement.subclasses

#Get just the subclass names
Achievement.subclasses.map(&:name)

然后对于显示 URL,可能会创建一条类似“achievements/:badge”的路由,然后在控制器中执行以下操作:

@badges = Achievement.where(:type => params[:badge]).all

#or, depending on how you've named everything
@badge = params[:badge].camelize.constantize.all

To iterate through the subclasses, you should be able to do something like this:

#Get the subclasses as class objects
Achievement.subclasses

#Get just the subclass names
Achievement.subclasses.map(&:name)

And then for the show URLs, probably make a route like 'achievements/:badge' and, in your controller, do

@badges = Achievement.where(:type => params[:badge]).all

#or, depending on how you've named everything
@badge = params[:badge].camelize.constantize.all
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文