[英]Toggling an admin user with a form RoR
I am looking at creating a toggle button for current admins to toggle user admin status on/off. 我正在为当前管理员创建一个切换按钮,以打开/关闭用户管理员状态。 From my understanding I will require some json to perform this but I am very limited in this category and am just a beginner to programming in general.
根据我的理解,我将需要一些json来执行此操作,但是我在此类别中非常有限,并且通常只是编程的初学者。 I am using rails v5.0.1 and ruby v2.3.2 Information about how I want this to work:
我正在使用Rails v5.0.1和ruby v2.3.2有关我希望它如何工作的信息:
An admin will access the Users Index where he/she will be able to search through all users 管理员将访问“用户索引”,在那里他/她将能够搜索所有用户
Each user can be deleted or admin on/off toggled via Administrative actions developed with before_actions (in controllers) and if/else methods (in views) 通过使用before_actions(在控制器中)和if / else方法(在视图中)开发的管理操作,可以删除每个用户或通过admin开/关切换每个用户
NOTE: My User table is name:string, email:string, and admin:boolean default:false (for obvious reasons). 注意:我的用户表是名称:字符串,电子邮件:字符串和管理:布尔默认值:假(出于明显的原因)。 though it doesn't matter the passwords are pass/passconfirm with a digest.
尽管没关系,密码是带有摘要的pass / passconfirm。
I have searched everywhere but I either get a routing issue when trying to send a patch request or a simple syntax error because I tried something new 我到处搜索过,但是尝试发送补丁请求时遇到路由问题,或者由于我尝试了新的操作而出现了简单的语法错误
Users Controller 用户控制器
class UsersController < ApplicationController
include UsersHelper
before_action :logged_in_user, only: [:edit, :update, :index]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [ :destroy]
def index
@users = User.paginate(page: params[:page])
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 #{@user.name}!"
redirect_to user_path(@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]= "#{@user.name} you have successfully updated account information"
redirect_to user_path(@user)
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def logged_in_user
unless logged_in?
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
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def make_admin
@user = User.find(params[:id])
@user.toggle!(:admin)
redirect_to users_path
end
end
and my users/index.html.erb: 和我的users / index.html.erb:
<h1>Users Index</h1>
<div class="container-fluid">
<div class="col-xs-6">
<%= will_paginate %>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %> |
<%= button_to [:make_admin, @user] do %>
Make <%= @user.name%> an admin?
<%= link_to "delete", user, method: :delete,
data: { confirm: "Are you sure?" } %> |
</li>
<% end %>
<% end %>
</ul>
<% end %>
<%= will_paginate %>
</div>
</div>
first don't let the method on private because with that, the method isn't accesible directly, that's the first part. 首先,不要让该方法私有,因为那样,该方法就不能直接访问,这是第一部分。 just put the method above the
private
line. 只需将方法放在
private
线上方即可。
and if you have for example in your routes a resources for user, add this 例如,如果您的路线中有用户资源,则添加此
resources :users do
match :make_admin, :via => [:get]
end
then let's go with the link 然后我们去链接
<%= link_to user_make_admin_path(user.id) do %>
Make <%= user.name%> an admin?
<% end%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.