2015-12-30 86 views
3

我目前遵循Michael Hartl的教程。我正在進行一些修改以適應我的應用程序。我需要包括的一項功能是,當用戶以管理員身份登錄時,他們被導向到不同的頁面,他們反過來可以創建或刪除更多的用戶等等。我在用戶表中創建了一個名爲admin和set的布爾值管理記錄爲TRUE。重定向管理員用戶到不同的頁面 - Ruby on Rails

這裏是我的users_controller.rb

class UsersController < ApplicationController 

    before_action :logged_in_user, only: [:index, :edit, :update] 
    before_action :correct_user, only: [:edit, :update] 


     def index 
     @users = User.all 
     end 

     def show 
     @user = User.find(params[:id]) 
     end 

     def new 
     @user = User.new 
     end 


     def create 
     @user = User.new(user_params) 
     if @user.save 
      log_in @user 
      flash[:success] = "Welcome to the Sample App!" 
      redirect_to @user 
     else 
      render 'new' 
     end 
     end 

     def edit 
     @user = User.find(params[:id]) 
     end 

     def update 
     @user = User.find(params[:id]) 
     if @user.update_attributes(user_params) 
      flash[:success] = "Profile updated" 
      redirect_to @user 
      # Handle a successful update. 
     else 
      render 'edit' 
     end 
     end 

     private 

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

     # Before filters 

     # Confirms a logged-in user. 
     def logged_in_user 
      unless logged_in? 
      store_location 
      flash[:danger] = "Please log in." 
      redirect_to login_url 
      end 
     end 


     # Confirms the correct user. 
     def correct_user 
      @user = User.find(params[:id]) 
      redirect_to(root_url) unless current_user?(@user) 
     end 


    end 

下面是我sessions_controller

class SessionsController < ApplicationController 
     def new 
     end 

     def create 
      user = User.find_by(email: params[:session][:email].downcase) 
     if user && user.authenticate(params[:session][:password]) 
      log_in user 
      params[:session][:remember_me] == '1' ? remember(user) : forget(user) 
      #remember user 
      #redirect_to user 
      redirect_back_or user 
      # Log the user in and redirect to the user's show page. 
     else 
      # Create an error message 
      flash.now[:danger] = 'Invalid email/password combination' 
      render 'new' 
     end 
     end 

     def destroy 
     log_out if logged_in? 
     redirect_to root_url 
     end 

    end 

下一個我sessions_helper

module SessionsHelper 

     # Logs in the given user. 
     def log_in(user) 
     session[:user_id] = user.id 
     end 

     # Remembers a user in a persistent session. 
     def remember(user) 
     user.remember 
     cookies.permanent.signed[:user_id] = user.id 
     cookies.permanent[:remember_token] = user.remember_token 
     end 

     # Returns the user corresponding to the remember token cookie. 
     def current_user 
     if (user_id = session[:user_id]) 
      @current_user ||= User.find_by(id: user_id) 
     elsif (user_id = cookies.signed[:user_id]) 
      user = User.find_by(id: user_id) 
      if user && user.authenticated?(cookies[:remember_token]) 
      log_in user 
      @current_user = user 
      end 
     end 
     end   def logged_in? 
     !current_user.nil? 
     end 

     # Logs out the current user. 
     def log_out 
     forget(current_user) 
     session.delete(:user_id) 
     @current_user = nil 
     end 

     # Forgets a persistent session. 
     def forget(user) 
     user.forget 
     cookies.delete(:user_id) 
     cookies.delete(:remember_token) 
     end 

     # Returns true if the given user is the current user. 
     def current_user?(user) 
     user == current_user 
     end 

     # Redirects to stored location (or to the default). 
     def redirect_back_or(default) 
     redirect_to(session[:forwarding_url] || default) 
     session.delete(:forwarding_url) 
     end 

     def redirect_to_admin_page 
     redirect_to 'index' 
     end 

     # Stores the URL trying to be accessed. 
     def store_location 
     session[:forwarding_url] = request.url if request.get? 
     end 

,最後我的路線文件

Rails.application.routes.draw do 

     get 'sessions/new' 

     root    'static_pages#home' 
     get 'static_pages/home' 
     get 'static_pages/landing' 
     get 'home' => 'static_pages#home' 
     get 'landing' => 'static_pages#landing' 
     get 'signup' => 'users#new' 
     get 'login' => 'sessions#new' 
     post 'login' => 'sessions#create' 
     delete 'logout' => 'sessions#destroy' 
     resources :users 
    end 

如果有人能指出我的方向是正確的,那就太棒了。

Rgrds 大衛

回答

0

,你必須

redirect_back_or user 

只是改變

if admin_user? # typically this is helper 
    redirect_to admin_page # adjust to your app.. i.e. where you want to go 
else 
    redirect_back_or user 
end 

哪裏admin_user?定義如下:

def admin_user? 
    current_user && current_user.admin?  
    # assuming admin is the name of your boolean field. 
end 

通常admin_user?會在同一個地方/途徑定義爲CURRENT_USER

+0

如果admin_user redirect_to_admin_page 其他 redirect_back_or用戶 結束 –

+0

嗨Micth,我如何定義admin_user在我的助手考慮到即時檢查它對我的表中的布爾值。謝謝 –

+0

感謝米奇我終於得到這個工作使用這個建議。我真的很感激。 –

0

像這個米奇幫手

 def create 
       user = User.find_by(email: params[:session][:email].downcase) 
      if user && user.authenticate(params[:session][:password]) 
      log_in user 
      params[:session][:remember_me] == '1' ? remember(user) : forget(user) 
      #remember user 
      #redirect_to user 
      if admin_user 
       redirect_to_admin_page 
      else 
       redirect_back_or user 
      end  
      # Log the user in and redirect to the user's show page. 
      else 
      # Create an error message 
      flash.now[:danger] = 'Invalid email/password combination' 
      render 'new' 
      end 
     end 
+0

上面的代碼給我一個語法錯誤。我是否可以嵌套If語句如下所示 –

0

看來你已經在模塊中的模塊SessionsHelper未申報的功能redirect_back_orSessionsHelper

一些相關的有用的功能定義如下:

def redirect_back_or(default) 
    redirect_to(session[:return_to] || default) 
    clear_return_to 
end 

def store_location 
    session[:return_to] = request.fullpath 
end 

def clear_return_to 
    session[:return_to] = nil 
end 

您可以使用store_location來保存當前拍子h並在用戶登錄時重定向用戶。

在你的情況下,它應該是這樣的:

def redirect_back_or(default) 
    if user.admin? 
     redirect_to admin_page # the page you want admin to redirect 
    else 
     redirect_to (session[:return_to] || default) 
    end 
end 
+0

Hi Seyna,我已經從會話幫助程序中意外排除了一些代碼,之後我添加了它 –

+0

很高興聽到您找到它。 – Seyna

0

在我的課程幫助我還沒有定義admin_user像這樣

 # Returns true if the given user is the current user. 
     def current_user?(user) 
     user == current_user 
     end 

     def admin_user? 
      current_user && current_user.admin?  
     end 


     # Redirects to stored location (or to the default). 
     def redirect_back_or(default) 
     redirect_to(session[:forwarding_url] || default) 
     session.delete(:forwarding_url) 
     end 

我在我的會話控制器創建方法現在看起來像這樣

def create 
       user = User.find_by(email: params[:session][:email].downcase) 
      if user && user.authenticate(params[:session][:password]) 
      log_in user 
      params[:session][:remember_me] == '1' ? remember(user) : forget(user) 
      #remember user 
      #redirect_to user 
       if admin_user 
        redirect_to root_url 
       else 
        redirect_back_or user 
       end 
      # Log the user in and redirect to the user's show page. 
      else 
      # Create an error message 
      flash.now[:danger] = 'Invalid email/password combination' 
      render 'new' 
      end 
     end 

但是我得到以下錯誤,當我嘗試登錄

NameError在SessionsController#創建 未定義的局部變量或方法`admin_user」爲#

相關問題