![](/img/trans.png)
[英]How can I make specific user(not current_user) sign out on rails-devise website?
[英]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.