如何使用Ruby on Rails创建管理员用户

时间:2014-07-22 06:49:13

标签: ruby-on-rails authentication admin

我是ruby on rails的新手,到目前为止,我创建了用户登录,注册,注销,但我想创建一个管理员用户,我不知道该怎么做。

route.rb是:

Rails.application.routes.draw do

  get 'courses/index'

  get 'courses/new'

  get 'courses/edit'

  get 'courses/show'

  get 'course/index'

  get 'course/new'

  get 'course/edit'

  get 'course/show'

  get 'sessions/signin'

  get 'sessions/main'

  get 'sessions/profile'

  get 'sessions/setting'

  get 'users/new'

  get 'home/index'

  resources :users, path_names: {new: "signup"} do
    collection do
      get "main", action: :new
    end
  end

  resources :sessions, path_names: {new: "signin", destroy: "signout"} do
    collection do
      get :signin
      get :main
      get :profile
      get :setting
    end
  end

  resources :home, only: [], path: "" do
    get :about
  end

  root 'home#index' 

  resources :courses, only: [:index, :new, :edit, :show]
  #resources :course, only: [:index, :new, :edit, :show]

     resources :courses

     resources :course_groups
     patch 'course_groups/:id/add_course', to: 'course_groups#add_course', as: 'course_group_add_course'
     delete 'course_groups/:id/delete_course', to: 'course_groups#destory_course', as: 'course_group_destory_course'


     resources :rules 
     patch 'rules/:id/add_group', to: 'rules#add_group', as: 'rule_add_course_group'
     delete 'rules/:id/delete_group', to: 'rules#destroy_group', as: 'rule_destroy_course_group'

     resources :requirements
     patch 'requirements/:id/add_rule', to: 'requirements#add_rule', as: 'requirement_add_rules'
     delete 'requirements/:id/delete_rule', to: 'requirements#destroy_rule', as: 'requirement_destroy_rules'


     #resources :plans     
     resources :users do
        resources :plans 
     end
     patch 'users/:user_id/plans/:id/add_course', to: 'plans#add_course', as: 'plan_add_course'
     delete 'users/:user_id/plans/:id/delete_course', to: 'plans#destory_course', as: 'plan_destory_course'


  match '/about', to: 'home#about', via: 'get'
  match '/signup', to: 'users#new', via: 'get'
  match ':controller(/:action(/:id))(.:format)', via: 'get'
  match '/signin', to: 'sessions#new', via: 'get'
  match '/signout', to: 'sessions#destroy', via: 'delete'
  match '/main', to: 'users#new', via: 'get'
  #match '/profile', to: 'sessions#profile', via: 'get'
  match '/setting', to: 'sessions#setting', via: 'get'


  match '/editname', to: 'users#edit', via: 'get'  

  match '/show', to: 'users#show', via: 'get'
  match '/profile', to: 'users#profile', via: 'get'
  #match '/info', to: 'users#info', via: 'get'
  #match '/changepass', to: 'users#edit', via: 'get'
end

我的users_controller.rb是:

class UsersController < ApplicationController

  before_filter :check_if_signned_in, only: [:show, :edit, :update, :delete] 

  before_filter :signed_in_user, only: [:edit, :update]

  before_filter :skip_password_attribute, only: :update

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user, notice: 'User was sucessfully created!'
    else
      render action: "new"
    end
  end


  #if current user exits, then it can get back to the main page of user
  def show
    if current_user.nil?
      @user = User.find(params[:id])
    else
      @user = User.find(current_user)  
    end

  end


def edit 
  #@user = User.find(params[:id])
  #@user = User.find(current_user)
  @user = current_user
  @user.save
  @title = "Edit profile"
end


def update
  @user = User.find(params[:id])
  #@user = User.find(current_user)
  #if @user.update_attributes(user_params)
  if @user.update_attributes(user_params)
    flash[:success] = "Profile updated."
    #sign_in @user_params
    redirect_to @user
  else
    @title = "Edit profile"
    render 'edit'
  end
end


  private
    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

    def signed_in_user
      redirect_to signin_path, notice: "Please sign in." unless signed_in?
    end

    def edit_user
      params.require(:user).permit(:name)
    end

    def skip_password_attribute
      if params[:password].blank? && params[:password_confirmation].blank?
        params.except!(:password, :password_confirmation)
      end
    end

end

application_controller.rb是:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  #protect_from_forgery with: :exception
  protect_from_forgery

  #before_filter :session_expire
  #before_filter :update_active_time

  include SessionsHelper

  def check_if_signned_in
    deny_access unless signed_in?
  end


#def update_active_time
#   session[:expires_at] = default_expire
#end

def is_owner
  current_user.id == params[:id]
end

#private
#   def get_session_time_left
#       expire_time = session[:expires_at] #|| default_expire
#       a = expire_time
#       b = Time.now
#       @session_time_left = (a - b).do_i
        #@session_time_left = (expire_time - Time.now).do_i
#   end


end

任何人都可以教我如何创建管理员用户吗?当admin用户登录时,它将转到另一个不同的页面。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

没有粗鲁,您的代码确实需要清理。

你的路线一团糟 - 我想我记得有一天会给你一些代码来帮助他们,看来你已经用过它了!但是,你还离开了其他路线 - 这可能会导致大问题


<强>路线

首先,您需要按顺序获取路线:

#config/routes.rb
resources :courses
resources :users, path_names: {new: "signup"} do
   collection do
      get "main", action: :new
   end
end

resources :sessions, path_names: {new: "signin", destroy: "signout", index: "main", edit: "profile", update: "setting"}

resources :home, only: [], path: "" do
   get :about
end

 root 'home#index' 

 resources :courses, only: [:index, :new, :edit, :show]
 resources :course_groups do
    patch :add_course
    delete :delete_course
 end


 resources :rules do
     patch :add_group
     delete :delete_group
 end 

 resources :requirements do
     patch :add_rule
     delete :delete_rule
 end

 resources :users do
    resources :plans do
       patch :add_course
       delete :delete_course
    end
 end


 match '/about', to: 'home#about', via: 'get'
 match '/signup', to: 'users#new', via: 'get'
 match ':controller(/:action(/:id))(.:format)', via: 'get'
 match '/signin', to: 'sessions#new', via: 'get'
 match '/signout', to: 'sessions#destroy', via: 'delete'
 match '/main', to: 'users#new', via: 'get'
 match '/setting', to: 'sessions#setting', via: 'get'
 match '/editname', to: 'users#edit', via: 'get'  

简单地说,Rails is resource / object - orientated中的所有内容。这包括路线,因此您应确保您拥有的每个route 围绕资源(这就是为什么您可以将路线定义为resources等等)

-

管理

要创建admin用户,您可以使用以下几种选项:

  
      
  1. 有一个单独的admin模型
  2.   
  3. admin模型
  4. 中创建User属性   

我强烈建议您在admin模型中添加User属性 - 将其设为boolean列,如下所示:

#new migration
add_column :users, :admin, :boolean

这将允许您调用以下

@user = User.find params[:id]
@user.admin? #-> true / false

这将允许您在控制器和放大器中使用多个条件语句。您的应用程序的视图,以确定用户是否具有admin权限