Rails,Devise - 管理员尝试编辑另一个用户个人资料,自己加载个人资料 [英] Rails, Devise - Admin trying to edit another user profile, own profile loaded instead

查看:91
本文介绍了Rails,Devise - 管理员尝试编辑另一个用户个人资料,自己加载个人资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发Rails时,我正在尝试着解决以下问题:
我正在与Devise + Cancan + olify应用程序一起尝试创建身份验证和用户管理。
我有一般的用户行为排序,我试图实现一个管理员用户可以编辑另一个用户配置文件。



此刻管理员用户可以列出用户并尝试编辑其他用户的个人资料。然而,在编辑页面上,尽管URL /路由对我来说是正确的,但我仍然以表单中的currentuser/ admin信息呈现。



所以简单来说,这个场景:
UserId1是一个管理员
UserId1正在尝试编辑UserId2的配置文件



记录为UserId1,以下路由带有UserId1而不是UserId2的详细信息:
http:// localhost:3000 / d / users / edit.2



这里是routes.rb:

  devise_for:users,:path_prefix => 'd',:controllers => {:registrations => 'registration'} 

命名空间:admin do
get'',to:'dashboard#index',as:'/'
resources:users
end

这是用户#index视图:

 <表> 
< tbody>
<%@ users.each do | user | %GT;
< tr>
< td><%= user.id%>< / td>
< td><%= user.first_name%>< / td>
< td><%= user.last_name%>< / td>
< td><%= user.email%>< / td>
< td>
<%= link_to edit_user_registration_path(user)%>
< / td>
< td>
<%= link_to registration_path(user),class:red,:data => {:confirm => 你确定? },:method => :删除%>
< / td>
< / tr>
<%end%>
< / tbody>
< / table>

这是users_controller.rb:

  class Admin :: UsersController< ApplicationController 
def index
@users = User.all
end
end

任何帮助都不胜感激!






编辑1



rake路线给了我以下内容:

 前缀动词URI模式控制器#动作
new_user_session GET /d/users/sign_in(.:format)devise / sessions#new
user_session POST /d/users/sign_in(.:format)devise / sessions#create
destroy_user_session DELETE /d/users/sign_out(.:format)devise / sessions#destroy
user_password POST /d/users/password(.:format)devise / passwords#create
new_user_password GET / d / users / password / new(。:format)devise / passwords#new
edit_user_password GET /d/users/password/edit(.:format)devise / passwords#edit
PATCH / d / users /密码(。:格式)devise / passwords#update
PUT /d/users/password(.:format)devise / passwords#update
cancel_user_registration GET /d/users/cancel(.:format)registrationments#cancel
user_registration POST /d/users(.:format)注册#创建
new_user_registration GET /d/users/sign_up(.:format)注册#new
edit_user_registration GET / d / users / edit(。 :格式)注册#编辑
PATCH /d/users(.:format)注册#update
PUT /d/users(.:format)注册#update
DELETE / d / users(创建
new_user_confirmation GET /d/users/confirmation/new(.:format)设计/ dev /确认#新
GET /d/users/confirmation(.:format)devise / confirmations#show
user_unlock POST /d/users/unlock(.:format)devise / unlocks#create
new_user_unlock GET / d / users / unlock / new(。:format)devise / unlocks#new
GET /d/users/unlock(.:format)devise / unlocks#show
root GET / pages#home
about GET /about(.:format)pages#about
admin GET /admin(.:format)admin / dashboard#index
admin_users GET /admin/users(.:format)admin / users #index
POST /admin/users(.:format)admin / users#create
new_admin_user GET /admin/users/new(.:format)admin / users#new
edit_admin_user GET / admin / users /:id / edit(。:format)a dmin / users#edit
admin_user GET /admin/users/:id(.:format)admin / users#show
PATCH /admin/users/:id(.:format)admin / users#update
PUT /admin/users/:id(.:format)admin / users#update
DELETE /admin/users/:id(.:format)admin / users#destroy


解决方案

 <%= link_to edit_user_registration_path )%> 

仍然指向您的设计路线,应该是:

 <%= link_to edit_admin_user_path(user)%> 

这是在耙子路线中描述的。


Being fairly new in developing with Rails, I'm trying to get my head around the following: I'm working with a Devise + Cancan + olify app to try and create authentication and user management. I've the general user behaviour sorted and I'm trying to achieve for an admin user to be able to edit another user profile.

At the moment, the admin user is able to list users and try to edit other user's profile. However, when going on the edit page, despite the URL/route being correct to me, I'm still being presented with the "currentuser"/admin information in the form.

So in short, the scenario: UserId1 is an admin UserId1 is trying to edit the profile of UserId2

Logged as UserId1, the following route bring details for UserId1 instead of UserId2: http://localhost:3000/d/users/edit.2

Here is the routes.rb:

devise_for :users, :path_prefix => 'd', :controllers => { :registrations => 'registrations' }

namespace :admin do
  get '', to: 'dashboard#index', as: '/'
  resources :users
end

Here is the users#index view:

<table>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.id %></td>
        <td><%= user.first_name %></td>
        <td><%= user.last_name %></td>
        <td><%= user.email %></td>
        <td>
          <%= link_to edit_user_registration_path(user) %>
        </td>
        <td>
          <%= link_to registration_path(user),class: "red", :data => { :confirm => "Are you sure?" }, :method => :delete %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

Here is the users_controller.rb:

class Admin::UsersController < ApplicationController
  def index
    @users = User.all
  end
end

Any help is appreciated!


Edit 1

rake routes gives me the following:

    Prefix Verb               URI Pattern                         Controller#Action
new_user_session          GET    /d/users/sign_in(.:format)          devise/sessions#new
user_session              POST   /d/users/sign_in(.:format)          devise/sessions#create
destroy_user_session      DELETE /d/users/sign_out(.:format)         devise/sessions#destroy
user_password             POST   /d/users/password(.:format)         devise/passwords#create
new_user_password         GET    /d/users/password/new(.:format)     devise/passwords#new
edit_user_password        GET    /d/users/password/edit(.:format)    devise/passwords#edit
                          PATCH  /d/users/password(.:format)         devise/passwords#update
                          PUT    /d/users/password(.:format)         devise/passwords#update
cancel_user_registration  GET    /d/users/cancel(.:format)           registrations#cancel
user_registration         POST   /d/users(.:format)                  registrations#create
new_user_registration     GET    /d/users/sign_up(.:format)          registrations#new
edit_user_registration    GET    /d/users/edit(.:format)             registrations#edit
                          PATCH  /d/users(.:format)                  registrations#update
                          PUT    /d/users(.:format)                  registrations#update
                          DELETE /d/users(.:format)                  registrations#destroy
user_confirmation         POST   /d/users/confirmation(.:format)     devise/confirmations#create
new_user_confirmation     GET    /d/users/confirmation/new(.:format) devise/confirmations#new
                          GET    /d/users/confirmation(.:format)     devise/confirmations#show
user_unlock               POST   /d/users/unlock(.:format)           devise/unlocks#create
new_user_unlock           GET    /d/users/unlock/new(.:format)       devise/unlocks#new
                          GET    /d/users/unlock(.:format)           devise/unlocks#show
root                      GET    /                                   pages#home
about                     GET    /about(.:format)                    pages#about
admin                     GET    /admin(.:format)                    admin/dashboard#index
admin_users               GET    /admin/users(.:format)              admin/users#index
                          POST   /admin/users(.:format)              admin/users#create
new_admin_user            GET    /admin/users/new(.:format)          admin/users#new
edit_admin_user           GET    /admin/users/:id/edit(.:format)     admin/users#edit
admin_user                GET    /admin/users/:id(.:format)          admin/users#show
                          PATCH  /admin/users/:id(.:format)          admin/users#update
                          PUT    /admin/users/:id(.:format)          admin/users#update
                          DELETE /admin/users/:id(.:format)          admin/users#destroy

解决方案

<%= link_to edit_user_registration_path(user) %>

Is still pointing to your devise route, it should be:

<%= link_to edit_admin_user_path(user) %>

Which is described in rake routes.

这篇关于Rails,Devise - 管理员尝试编辑另一个用户个人资料,自己加载个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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