目前,我允許用戶在我的導軌應用程序(類似於推特)上跟隨另一個。如何使用戶自動跟蹤管理用戶註冊
如果註冊到網站的新用戶自動關注管理員用戶,我很樂意。
Similar to how MySpace use to automatically make Tom your first friend
下面是我用它來創建新用戶,並允許用戶彼此相隨。代碼(我知道這是一個非常廣泛的問題,但.....)
(可有人請指出我正確的方向,我怎麼能得到這個開始。我需要創建一個方法....在我的模型或添加代碼到控制器?)
新來的Rails請幫忙).. 。:)
個USER控制器
class UsersController < ApplicationController
before_filter :admin_user, only: [:destroy]
respond_to :html, :js
def new
@user = RegularUser.new
end
def index
@users = User.paginate(page: params[:page], :per_page => 100).search(params[:search])
end
def destroy
User.find_by_username(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
def create
@user = RegularUser.new(params[:regular_user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
UserMailer.welcome_user(@user).deliver
sign_in @user
flash[:success] = "Welcome to the ClickOnComics!"
redirect_to (publishers_path)
else
render 'new'
end
end
private
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def follow_admins
admins = User.find_by_admin(true)
admins.each do |admin|
self.follow!(admin)
end
end
class RelationshipsController < ApplicationController
before_filter :current_user
respond_to :html, :js
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_with @user
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_with @user
end
end
模型
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
after_create :follow_admins
def follow_admins
admins = User.find_all_by_admin(true)
admins.each do |admin|
self.follow!(admin)
end
end
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
end
我用這個教程,以幫助我建立特權管理用戶與用戶模式
http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-administrative_users
一個布爾管理屬性SCHEMA
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.string "role"
t.string "username"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.boolean "admin", :default => false
t.string "password_reset_token"
t.timestamp "password_reset_sent_at"
end
我需要創建一個定義user_admin方法?
我已經在上面添加了一些代碼來解釋我如何建立管理員權限。當我將代碼添加到user.rb(未定義的局部變量或方法'admin_user')時出現此錯誤我需要創建一個定義admin_user的方法嗎? –