将管理员用户重定向到不同的页面 - Ruby on Rails

时间:2015-12-30 15:29:18

标签: ruby-on-rails

我目前正在关注Michael Hartl的教程。我正在进行一些修改以适应我的应用程序。我需要包含的一项功能是,当用户以管理员身份登录时,他们将被定向到另一个页面,在那里他们可以创建或删除更多用户等。我在users表中创建了一个名为admin和set的布尔值管理员记录为TRUE。

这是我的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 next

    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 大卫

4 个答案:

答案 0 :(得分: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

相同的位置/方式定义的帮助器

答案 1 :(得分:0)

喜欢这个Mitch

        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

答案 2 :(得分:0)

您似乎在模块redirect_back_or

中有未声明的函数SessionsHelper

模块SessionsHelper中定义的一些相关有用函数是:

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保存当前路径并在用户登录时重定向用户。

在您的情况下,它应该如下所示:

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

答案 3 :(得分: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

但是,当我尝试登录时,我收到以下错误

SessionsController中的NameError #create 未定义的局部变量或方法`admin_user&#39;对于#