在devise中将其他用户编辑为admin,ruby on rails [英] Edit other user as admin in devise, ruby on rails

查看:38
本文介绍了在devise中将其他用户编辑为admin,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:

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:


  1. 在devise Wiki中有一个条目从2011年开始: https:/ /github.com/plataformatec/devise/wiki/How-To%3a-Manage-users-through-a-CRUD-interface

  1. There is an entry in the devise wiki from 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.


  1. 对此有一个stackoverflow答案:设计:允许管理员编辑其他用户-Rails

  1. There is a stackoverflow answer to this: Devise: Allow admins to edit other users - Rails

但是我不完全了解,需要做什么:我知道,我需要添加 devise_for:users,:path_prefix => 到达我的路线,但发帖人还谈到了自己构建表单和控制器,以及隔离Userscontroller。我不明白他的意思。

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.


  1. 还有另一个stackoverflow答案:Rails,Devise-管理员试图编辑另一个用户个人资料,而是加载自己的个人资料

  1. There is another stackoverflow answer: Rails, Devise - Admin trying to edit another user profile, own profile loaded instead

在此文件中,海报使用的是康康舞,看来他确实有一个他没有的usercontroller中的admin类:

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

提前感谢!

推荐答案

我知道了:


  1. devise_for:users,:: path_prefix => 'my'

  2. 在devise_for中添加以下内容: resources:users

  3. 从注册向用户复制edit.html.erb。

  4. 更改<%= form_for(resource,as:resource_name,url:registration_path (resource_name),html:{方法::put})做| f | %>

  1. add a prefix in routes on devise_for :users,: :path_prefix => 'my'
  2. add this below devise_for: resources :users
  3. copy edit.html.erb from the registration from devise to users.
  4. Change <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

至:<%= form_for(@user)做| f | %>


  1. 将此添加到用户控制器:

  1. 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)

这篇关于在devise中将其他用户编辑为admin,ruby on rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆