简体   繁体   English

如何使Rails作者资源成为设计用户?

[英]How can I make a rails authors resource a devise user?

I have setup a rails application with devise on it so users can register and login and they can add posts and other resources based on their roles(Admin,Teacher,Author,Pupil). 我已经设置了一个带有设计的Rails应用程序,以便用户可以注册和登录,并且他们可以根据自己的角色(管理员,老师,作者,学生)添加帖子和其他资源。 Admin as access to everything and I am using gem 'cancan' for roles. 以对所有内容的访问权限进行管理,而我使用的是“ 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

Currently Authors are just added to a book when you create the book. 当前,当您创建书时,作者仅被添加到书中。 In the backend you just add authors as many as you like and then assign that to a book. 在后端,您只需添加任意数量的作者,然后将其分配给一本书即可。 If your an admin you can edit authors details and if your an author you can edit your own profile area in your author resource but there is no login for an author to do this at all. 如果您是管理员,则可以编辑作者的详细信息,如果您是作者,则可以在作者资源中编辑自己的个人资料区域,但是根本没有登录者可以执行此操作。

What I want to know is: Is it easy to create a devise user type like author from authors already added to the backend or will they need to be created all over again? 我想知道的是:从已经添加到后端的作者中创建像author这样的设计用户类型是否容易,还是需要重新创建?

=== Edit below here ==== ===在此处编辑下面====

user.rb 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

role.rb role.rb

class Role < ActiveRecord::Base
    has_many :users
end

author.rb 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

Thanks Mark 谢谢马克

You don't need to create separate model(Author) just to handle author logins. 您无需创建单独的模型(作者)即可处理作者登录。 Better approach will be using associations in this case. 在这种情况下,更好的方法是使用关联。

 #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)

now, use this user object and go for devise login. 现在,使用此用户对象并进行设计登录。

Now to list all the authors of our Application. 现在列出我们应用程序的所有作者。

   #user.rb
      def self.all_authors
          User.select('users.id, users.name, user_roles.role AS USER_ROLE')
         .joins(:user_role).where(user_roles: {role: 'Author'})
      end

Now to get all authors just give a call : 现在要让所有作者打电话:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM