Ruby on rails 如何使rails作者资源成为Desive用户?

Ruby on rails 如何使rails作者资源成为Desive用户?,ruby-on-rails,ruby,ruby-on-rails-4,devise,cancan,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,Cancan,我已经安装了一个带有Desive的rails应用程序,用户可以注册和登录,他们可以根据自己的角色(管理员、教师、作者、学生)添加帖子和其他资源。管理员可以访问所有内容,我使用gem‘cancan’作为角色 if user.role if user.role.name == "Admin" can :manage, :all elsif user.role.name == "Teacher" can :manage,

我已经安装了一个带有Desive的rails应用程序,用户可以注册和登录,他们可以根据自己的角色(管理员、教师、作者、学生)添加帖子和其他资源。管理员可以访问所有内容,我使用gem‘cancan’作为角色

if user.role
        if  user.role.name == "Admin"
            can :manage, :all
        elsif user.role.name == "Teacher"
            can :manage, [Book, Story]
            can :read, [Book, Author, Story]
        elsif user.role.name == "Author"
            can :manage, [Book, Author]
            can :read, [Book, Author, Story]
        elsif user.role.name == "Pupil"
            can :manage, [Story]
            can :read, [Book, Author, Story]
        elsif user
            can :read, :all
        end
    else
        can :read, :all
    end
当前,当您创建书籍时,作者只是添加到书籍中。在后端,您只需添加任意数量的作者,然后将其分配给一本书。如果你是管理员,你可以编辑作者的详细信息,如果你是作者,你可以在你的作者资源中编辑你自己的个人资料区域,但作者根本没有登录名

我想知道的是:从已经添加到后端的作者中创建类似author的designe用户类型容易吗?还是需要重新创建

==在此处编辑下面的内容====

user.rb

class User < ActiveRecord::Base
  belongs_to :role
  def confirmation_required?
    false
  end
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable


  has_attached_file :avatar, styles: {
        large: "600x450#",
        medium: "250x250#",
        small: "100x100#"
    }, :default_url => "/images/:style/filler.png"
  #validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
  validates :avatar, :email, :username, :password, presence: true

end
class用户“/images/:style/filler.png”
#验证附件内容类型:头像,内容类型=>[“image/jpg”,“image/jpeg”,“image/png”,“image/gif”]
验证\u附件\u内容\u类型:头像,:内容\u类型=>/\Aimage\/.\Z/
验证:化身、:电子邮件、:用户名、:密码、状态:真
结束
role.rb

class Role < ActiveRecord::Base
    has_many :users
end
类角色
author.rb

class Author < ActiveRecord::Base
    has_many :books, dependent: :destroy
    has_many :ideas, dependent: :destroy
    accepts_nested_attributes_for :books
    accepts_nested_attributes_for :ideas
    validates_uniqueness_of :name

    has_attached_file :avatar, :styles => { :large => "980x760", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/


end
类作者{:large=>“980x760”,:medium=>“300x300>”,:thumb=>“100x100>”},:default\u url=>“/images/:style/missing.png”
验证\u附件\u内容\u类型:头像,:内容\u类型=>/\Aimage\/.\Z/
结束
谢谢
标记

您不需要创建单独的模型(作者)来处理作者登录。 在这种情况下,更好的方法是使用关联

 #user_role.rb 
class UserRole < ActiveRecord::Base
  has_many :users
end

 #user.rb 
Class User < ActiveRecord::Base  
 belongs_to :user_role 
end

Typical user model attributes will be 
User(id: integer, email: string, name:string, user_role_id:integer)
user_role model attributes will be 
UserRole(id: integer, role: string)

UserRole.create(role: 'Admin') 
UserRole.create(role: 'Teacher')
UserRole.create(role: 'Author')
UserRole.create(role: 'Pupil')

then create your author( Let's assume the id for 'Author' role is 3)
user = User.create(name: 'abc', email: 'def@gmail.com', user_role_id: 3)
现在要让所有作者都参与进来,只需打个电话:

 User.all_authors #this will list all the users of type 'author'

嘿,Ajay,我已经用我的user.rb和role.rb编辑了我上面的问题,我想知道这些是否可以更改以适应您在回答中所说的内容?请稍等,我会检查并让您知道。您不需要单独的作者模型。只要用户模型就足够了。无论您在author中有什么关联,您都可以简单地将它们移动到用户模型。是的,因为作者是用户,但他/她只是由角色author定义的作者。。。是吗?还有一件事,我怎样才能把网站上所有的作者都导出到这个新的逻辑中呢?它们都在postgres数据库中设置为作者资源。
 User.all_authors #this will list all the users of type 'author'