如何设计与多种型号或角色合作? (优选用CanCan和rolify) [英] How to get devise to work with multiple models or roles? (Preferably with CanCan and rolify)

查看:144
本文介绍了如何设计与多种型号或角色合作? (优选用CanCan和rolify)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用设计,有多种型号或角色的最佳方法是什么?
以下是我应用中需要的模型或角色:

   - 作者:可以将内容上传到网站
- 客户:每月支付费用以访问内容

一个用户可以是一个作者和客户。我认为他们可以共享相同的登录表单,因为他们将使用他们的电子邮件地址登录。



我尝试使用 CanCan rolify ,了解如何在注册期间添加不同的角色。当用户注册为作者时,他应该被赋予作者角色,当用户通过支付每月费用进行注册时,应该被赋予客户角色。我也不想为角色使用复选框。



有一个名为作者的导航链接,允许作者注册,然后有一个链接,用户可以通过付款和结算表单进行注册。根据哪个链接被点击,应该确定给出哪个角色。在注册期间,我应该在url中传递角色参数,如user / sign_up / index?role = customer?如果是这样,在注册过程中如何设计使用?



使用devise,CanCan和rolify,我该如何解决?我认为有一个User类可能会更好,然后客户和作者从用户扩展,但是从StackOverflow读取很多类似的问题,它似乎更容易。



我知道我想做的是非常基本的,我还没有想到。我一直在试图为这一天找到一个解决方案。



更新:
丹尼的回答让我在正确的方向并且我通过传递URLusers / sign_up?role = customer或users / sign_up?role = author中的参数,设法在注册期间正确添加角色。我不知道这是否是最好的方法,但这是我迄今为止所做的。



我有一个问题是,我的has_many关系如何工作?这是我有的:

  class User< ActiveRecord :: Base 
rolify
devise:database_authenticatable,:可注册,
:可恢复,可记忆,可追踪,可验证

has_one:plan
has_many:files

end




has_one:计划是为客户的订阅计划
和has_many:文件
是为作者谁可以上传许多文件,客户可以
下载


问题是我还想跟踪用户的文件下载,所以我想把 has_many:downloads 或者可能 has_many:files,:through => :下载



如果将这些内容放在用户模型中,知道不是客户的作者不允许下载文件而不是作者的客户不能上传文件?

解决方案

从你的问题,我明白你已经通常,您可以在seed.rb文件中创建不同的角色,其中包含一些类似于

  [:admin,:author,:customer] .each do | role | 
Role.find_or_create_by_name({name:role},without_protection:true)
end

当您以管理员身份创建自己时,您也可以在seed.rb文件中使用

 用户分配:admin角色.add_role:admin 

对于您的其他角色,您可以将其分配给适当时,与您完全一样描述。当用户单击Authors链接并继续时,这将触发某些控制器中的某些操作。您可以通过使用相同的

  user.add_role:author 

您还可以分配连接到某些对象的角色。例如。作者只是作者自己创作的文件。在这种情况下,您不想分配一般作者角色,但您将分配如

  user.add_role :在控制器的创建操作中,作者,文档

以及保存创建的文档。这种作业的替代方法是在模型的回调中进行。



更多问题?只要问!


Using devise, what is the best way to have multiple models or roles? Here are the models or roles I need in my app:

-Author: can upload content to the site
-Customer: pays a monthly fee to access the content

A user can be both an Author and a Customer. I think they can share the same login form because they will both log in with their email address.

I have tried using CanCan and rolify, but couldn't figure out how to add different roles during registration. When a user registers as an Author, he should be given the "author" role and when a user registers by paying the monthly fee, he should be given the "customer" role. I don't want to use a checkbox for the roles either.

There is a nav link called "Authors" that will allow authors to register and then there is a link for users to register by going through the payment and billing form. Based on which link is clicked, should determine which role is given. Should I pass in a role parameter in the url during registration like "user/sign_up/index?role=customer"? If so, how do I get devise to use this in the registration process?

Using devise, CanCan, and rolify, how can I solve this? I thought it might be better to have a User class and then have Customer and Author extend from User, but from reading many similar questions on StackOverflow it seems rolify is easier.

I know what I am trying to do is very basic, I just haven't figured it out yet. I have been trying to find a solution for this all day.

Update: Danny's answer below pushed me in the right direction and I have managed to get the roles added properly during registration by passing in a parameter in the URL "users/sign_up?role=customer" or "users/sign_up?role=author". I don't know if this is the best way, but that's what I have so far.

One question I have though is how will my has_many relations work now? Here is what I have:

class User < ActiveRecord::Base
    rolify
    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

    has_one :plan
    has_many :files

end

has_one :plan is for the customer's subscription plan and has_many :files is for the author who can upload many files that the customer can download

The problem is that I would also like to keep track of the user's file downloads so I would like to put has_many :downloads or probably has_many :files, :through => :downloads

Does it matter if I put these in the User model knowing that an author who is not a customer is not allowed to download files and that a customer who is not an author cannot upload files?

解决方案

From your question, I understand you've already "dived" into rolify and cancan, so I'll focus on assigning roles.

You typically create different roles in your seeds.rb file, with something like

[:admin, :author, :customer].each do |role|
  Role.find_or_create_by_name({ name: role }, without_protection: true)
end

You assign the :admin role immediately when you create yourself as administrator, also in the seeds.rb file, using

user.add_role :admin

For your other roles, you assign them "when it's appropriate", exactly as you describe. When a user clicks the Authors link, and proceeds, this triggers some action in some controller. It is there that you assign this role to that user, by using the same

user.add_role :author

You can also assign roles, connected to certain objects only. E.g. an author is author for only the documents he creates himself. In that case you don't want to assign a "general" author role, but you will assign it like

user.add_role :author, document

in the controller's create action, together with saving the created document. Alternative for this kind of assignment is to do it in a callback from your model.

More questions? Just ask!

这篇关于如何设计与多种型号或角色合作? (优选用CanCan和rolify)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆