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
感謝 馬克
嘿阿賈伊的,我已經編輯上面我user.rb和role.rb我的問題,我不知道如果這些能要改變以符合你在回答中所說的內容? –
只是一會兒,會檢查並讓你知道。 – Ajay
您不需要單獨的作者模型。只需用戶模型就足夠了。 無論您在作者中擁有何種關聯,您都可以簡單地將它們移動到用戶模型中。 – Ajay