我目前遵循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 大衛
如果admin_user redirect_to_admin_page 其他 redirect_back_or用戶 結束 –
嗨Micth,我如何定義admin_user在我的助手考慮到即時檢查它對我的表中的布爾值。謝謝 –
感謝米奇我終於得到這個工作使用這個建議。我真的很感激。 –