有关如何在免费增值应用程序中实施限制的任何提示?

发布于 2024-12-11 09:20:14 字数 902 浏览 0 评论 0原文

我是一个苦苦挣扎的编程新手,并试图制作一个具有多个可用计划的免费增值风格的应用程序。

我使用康康宝石根据场馆的计划(免费、付费、付费+)为场馆分配角色。

我知道如何根据分配的计划限制视图中显示的某些选项,但如何根据分配的计划限制添加到场地的记录数量?

例如,

  • 我希望所有场馆都有照片,但免费场馆应该 仅限 3 个。
  • 我还希望所有场馆都有可搜索的标签,但允许的标签数量会根据场馆所处的计划而变化。

目前,这些计划只是在场地模型中指定为:

PLANS = %w[free premium premium+]

编辑

感谢 Alex Peattie 的回答,我认为我现在走在正确的轨道上。

我将 validate_on_create :photo_count_within_limit 行更改为:

validate :venuephoto_count_within_limit, :on => :create

但在 VenuesController#update 中收到 ArgumentError:Fixnum 与 nil 失败错误的比较。

另一个编辑

所以现在一切都很好,只是做了这些更改:

def photo_limit
  {:free => 3, :premium => 10}[plan.to_sym]
end

非常感谢

validate :venuephoto_count_within_limit, :on => :create

您的帮助!

I'm a struggling newbie to programming and trying to make a freemium style app with multiple plans available.

I'm using the cancan gem to assign venues a role depending on what plan they are on (free, premium, premium+).

I know how I can limit certain options being displayed in the views depending on which plan is assigned but how can I go about limiting number of records added to a venue depending on which plan is assigned?

e.g.

  • I would like all venues to have photos but the free venues should be
    limited to just 3.
  • I would also like all venues to have searchable tags but have the number of tags allowed change depending on which plan the venue is on.

Currently the plans are just specified in the venue model as:

PLANS = %w[free premium premium+]

edit

Thanks to the answer from Alex Peattie I think I'm on the right track now.

I changed the line validate_on_create :photo_count_within_limit to:

validate :venuephoto_count_within_limit, :on => :create

but am getting a ArgumentError in VenuesController#update: comparison of Fixnum with nil failed error.

another edit

So all is now well, just made these changes:

def photo_limit
  {:free => 3, :premium => 10}[plan.to_sym]
end

and

validate :venuephoto_count_within_limit, :on => :create

Thanks for any help its much appreciated!

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

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

发布评论

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

评论(2

久光 2024-12-18 09:20:14

我会为 photo_limittag_limit 等提供场地模型方法

class Venue < ActiveRecord::Base

  def photo_limit
    {:free => 3, :premium => 20, :"premium+" => 999}[plan]
  end

end

(这假设有一个 .plan 方法,该方法将返回当前场地的计划)

然后在 Photo 中使用验证,如下所示:

class Photos < ActiveRecord::Base

  belong_to :venue
  validate_on_create :photo_count_within_limit

  def photo_count_within_limit
    if self.venue.photos(:reload).count >= self.venue.photo_limit
      errors.add(:base, "Exceeded venue photo limit")
    end
  end

end

I'd give the Venue model methods for photo_limit, tag_limit etc.

class Venue < ActiveRecord::Base

  def photo_limit
    {:free => 3, :premium => 20, :"premium+" => 999}[plan]
  end

end

(this assumes a .plan method which will return the current Venue's plan)

Then use validations in Photo, like so:

class Photos < ActiveRecord::Base

  belong_to :venue
  validate_on_create :photo_count_within_limit

  def photo_count_within_limit
    if self.venue.photos(:reload).count >= self.venue.photo_limit
      errors.add(:base, "Exceeded venue photo limit")
    end
  end

end
青衫儰鉨ミ守葔 2024-12-18 09:20:14

在我目前的项目中,我们有同样的问题。我没有参与这部分,所以我不知道它是如何解决的。也许在 ApplicationController 上使用某种全局过滤器来检查用户当前计划/子计划(是的,我们也有子计划)的权限。

周一我会尝试找一些时间来检查我们如何实施这一点并回答您。

On my current project we have the same question. I haven't participate on that part, so i have no idea how it have been resolved. Maybe with some kind of global filter on the ApplicationController that checks for permissions on the current plan/subplan (yes, we have subplan too) of the user.

On monday i'll try to find some time to check how we implement this and answer you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文