繁体   English   中英

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

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

我已经设置了一个带有设计的Rails应用程序,以便用户可以注册和登录,并且他们可以根据自己的角色(管理员,老师,作者,学生)添加帖子和其他资源。 以对所有内容的访问权限进行管理,而我使用的是“ 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这样的设计用户类型是否容易,还是需要重新创建?

===在此处编辑下面====

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

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

谢谢马克

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

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

现在要让所有作者打电话:

 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