如何让rails作者资源成为设计用户?

时间:2015-01-13 16:12:50

标签: ruby-on-rails ruby ruby-on-rails-4 devise cancan

我已经设置了一个带有设计的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

目前,作者只是在您创建图书时添加到图书中。在后端,您只需添加任意数量的作者,然后将其分配给一本书。如果您是管理员,您可以编辑作者详细信息,如果您是作者,则可以在作者资源中编辑自己的个人资料区域,但作者根本没有登录信息。

我想知道的是:从已经添加到后端的作者创建像作者这样的设计用户类型是否容易?或者是否需要重新创建它们?

===在这下面编辑====

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

由于 标记

1 个答案:

答案 0 :(得分:0)

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

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