且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails,Devise - 管理员尝试编辑另一个用户个人资料,自己加载个人资料

更新时间:2023-09-28 17:21:28

 <%= 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.