2015-01-13 60 views
0

我已經設置了一個rails應用程序,它可以讓用戶註冊並登錄,他們可以根據角色(Admin,Teacher,Author,Pupil)添加帖子和其他資源。管理員可以訪問所有內容,並且我正在使用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 

感謝 馬克

回答

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: '[email protected]', 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' 
+0

嘿阿賈伊的,我已經編輯上面我user.rb和role.rb我的問題,我不知道如果這些能要改變以符合你在回答中所說的內容? –

+0

只是一會兒,會檢查並讓你知道。 – Ajay

+0

您不需要單獨的作者模型。只需用戶模型就足夠了。 無論您在作者中擁有何種關聯,您都可以簡單地將它們移動到用戶模型中。 – Ajay

相關問題