在设计中以管理员身份编辑其他用户,ruby on rails [英] Edit other user as admin in devise, ruby on rails
问题描述
我在我的 Ruby on Rails 项目中使用了 devise,并且用户有一个管理员属性:
I'm using devise in my Ruby on Rails project and users have an admin-attribute:
schema.rb:
create_table "users", force: :cascade do |t|
.
t.boolean "admin", default: false
end
我希望管理员用户可以编辑其他用户.我不是第一个问这个问题的人,但所有给出的答案都缺乏明确的说明:
I want, that an admin user can edit other users. I'm not the first one asking this question, but all the given answers lack clear instructions:
- devise wiki 中有一个 2011 年的条目:https://github.com/plataformatec/devise/wiki/How-To%3a-Manage-users-through-a-CRUD-interface
但我不明白我需要做什么:它在路由中提到了 "devise_for :users, :path_prefix => 'my'"
,但我没有我的路线.然后,它提到,我需要如何删除 params 哈希的密码密钥,但我的 users_controller 为空.不再提供更多说明.
But I don't understand what I need to do: It mentions "devise_for :users, :path_prefix => 'my'"
in the routes, but I don't have that in my routes. Then, it mentions, how I need to remove the password key of the params hash, but my users_controller is empty. No more instructions are provided.
- 对此有一个 stackoverflow 答案:设计:允许管理员编辑其他用户 - Rails
但我不完全明白,需要做什么:我明白了,我需要添加 devise_for :users, :path_prefix =>'d'
到我的路线,但海报还谈到了自己构建表单和控制器,以及隔离用户控制器.我不明白,他是什么意思.
But I don't fully understand, what needs to be done: I see, that I need to add devise_for :users, :path_prefix => 'd'
to my routes, but the poster talks also about building out your forms and controller on your own, and isolating the Userscontroller. I don't understand, what he means.
- 还有另一个 stackoverflow 答案:Rails, Devise - 管理员试图编辑另一个用户配置文件,而是加载了自己的配置文件
在这一个中,海报使用了 cancan,而且他的用户控制器中似乎确实有一个管理类,而我没有:
In this one, the poster uses cancan and it seems, he does have an admin class in his usercontroller, which I don't have:
class Admin::UsersController < ApplicationController
def index
@users = User.all
end
end
再一次,我不知道海报是如何到达那里的,他做了什么.
Once again, I don't know, how the poster got there and what he did.
是否有让管理员用户编辑其他用户配置文件的分步指南?
Is there a step-by-step guide, to let the admin-user edit other userprofiles?
您可以在此处访问我的代码:https://github.com/Metaphysiker/philosophica
You can access my code here: https://github.com/Metaphysiker/philosophica
提前致谢!
推荐答案
我想通了:
- 在
devise_for :users,: :path_prefix => 的路由中添加前缀'我的'
- 在下面添加devise_for:
resources :users
- 将 edit.html.erb 从注册从设计复制到用户.
- 更改
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|%>
to: <%= form_for(@user) do |f|%>
将此添加到用户控制器:
Add this to the usercontroller:
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to adminpanel_path
else
render 'edit'
end
end
完成.(奇怪的是,我不能在代码中输入数字 5)
这篇关于在设计中以管理员身份编辑其他用户,ruby on rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!