如何使用户自动注册后跟随管理员用户 [英] How to make Users automatically Follow Admin User on Sign Up

查看:94
本文介绍了如何使用户自动注册后跟随管理员用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我允许用户在我的Rails应用程序(类似于twitter)上彼此关注。

Currently I allow users to follow one another on my rails app (similar to twitter).

如果注册该网站的新用户自动关注管理员用户,我会很高兴。

Similar to how MySpace use to automatically make Tom your first friend

下面是我用来创建新用户并允许用户彼此关注的代码。(我知道这是一个非常广泛的问题,但.....)

Below is the code I use to create new users and allow users to follow one another.(i know this is a very broad question but .....)

(有人可以向我指出正确的方向。我是否需要在模型中创建方法...或向控制器添加代码?)

Rails新手请帮助)...:)

New to Rails Please help)... :)

用户控制器

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

我使用了本教程来帮助我在用户模型中建立具有布尔admin属性的特权管理用户

< a href = http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-administrative_users rel = nofollow> http://ruby.railstutorial.org/chapters/updating-显示和删除用户#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的方法?

推荐答案

在用户中.rb添加一个after_create过滤器

In user.rb add a after_create filter

after_create :follow_admin!

def follow_admin!
  relationships.create!(followed_id: admin_user.id)
end

这篇关于如何使用户自动注册后跟随管理员用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆