2014-07-22 138 views
2

我是Ruby on Rails的新手,迄今爲止,我創建了用戶登錄,註冊,註銷,但我想創建一個管理員用戶,但我不知道這個怎麼做。如何使用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 

任何人都可以教我如何創建一個管理員用戶嗎?當管理員用戶登錄時,它將轉到另一個不同的頁面。 提前謝謝!

回答

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. User模型創建admin屬性

我會強烈建議您在User模型中的admin屬性 - 使之成爲boolean列如下:

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

這將允許您撥打以下電話

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

這將允許你使用兩個控制器的幾個條件語句&意見您的應用程序,或以確定用戶是否有權限admin

相關問題