gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:48:40 26 4
gpt4 key购买 nike

我目前正在关注 Michael Hartl 的教程。我正在做一些修改以适合我的应用程序。我需要包含的一项功能是,当用户以管理员身份登录时,他们会被定向到不同的页面,在那里他们又可以创建或删除更多用户等。我在用户表中创建了一个名为 admin 的 bool 值并设置管理员记录为 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

下面是我的 session_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

我的 session_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

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

规则
大卫

最佳答案

你在哪里

 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 相同的地方/方式定义的助手

关于ruby-on-rails - 将管理员用户重定向到不同的页面 - Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com