使用Devise扩展更新不同用户的信息

Update information of different user with extension of Devise

我设置了一个平台,使用 Devise 创建和管理用户,使用 CanCanCan 管理权限。用户具有允许他们访问特定区域的角色(管理员或客户)。管理员用户可以 can :manage, :all

我扩展了 Devise,因此我有一个 users/registrations 和 users/sessions 控制器。

管理员用户可以访问 ManagerController 仪表板中的视图,从而允许他们管理其他用户。我已经想出如何显示不同用户的详细信息,但我不知道如何保存这些信息。我收到的错误表明它要么找不到用户,要么需要 POST 路由。

我是 Rails 的新手,所以它可能很简单。我能够找到包含常规 Devise 安装但不包含扩展的解决方案。我觉得问题可能出在我的路线上。

我也无法从这里创建新用户。

Routes.rb

  get 'users' => 'manager#users'
  get 'users/edit' => 'manager#edit'
  post 'users/edit' => 'manager#edit'
  get 'users/new' => 'manager#new'

  devise_for :users, :controllers => { registrations: 'users/registrations', sessions: 'users/sessions' }

  devise_scope :user do
      authenticated :user do
        root 'users/registrations#edit', as: :authenticated_root
      end

      unauthenticated do
        root 'users/sessions#new', as: :unauthenticated_root
      end
  end

经理控制器

class ManagerController < ApplicationController
  authorize_resource :class => false
  include ManagerHelper

  def users
     @users = User.where.not(:id => current_user.id)
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:notice] = "Successfully created User." 
      redirect_to root_path
    else
      render :action => 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
    @companies = Company.all

  end

  def update
    @user = User.find(params[:id])
    params[:user][:id].delete(:password) if params[:user][:password].blank?
    params[:user][:id].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?

    if @user.update(user_params)
      flash[:notice] = "Successfully updated User."
      redirect_to root_path
    else
      render :action => 'edit'
    end
  end

  end 
  private

  def user_params
     params.require(:user).permit(:email, :first_name, :last_name, :company_id, :role, :password, :password_confirmation)
  end
end

经理助手

module ManagerHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

Manager/Edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2>



<%= form_for(resource, :as => @user, :url => edit_user_registration_path(resource_name))  do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <%= f.hidden_field :id %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name%>
  </div>

  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>




    <div class="field">
      <%= f.label :company %><br />
      <%= f.collection_select :company_id, @companies, :id, :name, prompt: true %>
    </div>

    <div class="field">
      <%= f.label :role %><br />
      <%= f.text_field :role %>
    </div>



  <div class="field">
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "new-password" %>
    <% if @minimum_password_length %>
      <br />
      <em><%= @minimum_password_length %> characters minimum</em>
    <% end %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>


我希望将更新后的信息保存到用户数据库中。

目前,我在更改用户信息后单击更新时出现此错误。

ActiveRecord::RecordNotFound in ManagerController#edit Couldn't find User without an ID Weirdly, when I see the User info, the url is: http://localhost:3000/users/edit?id=2 But when I update (and get the error), the url changes to: http://localhost:3000/users/edit.user

我还希望能够从此屏幕创建新用户。 目前,用户表单填充了登录用户的信息。(公平地说,在解决这个问题之前,我一直在努力让编辑用户正确。)

我认为您想更新现有的用户信息。 如果是这样的话: 我发现你的 url 路径是错误的

:url => edit_user_registration_path(resource_name)

您不必在此处进行用户注册。 如果管理员要更新信息 在路线文件中。像这样的东西

namespace :admins do
    resources :users
end

然后使用由此创建的路径

经过多次尝试,我找到了解决办法。有很多错误,所以请耐心等待。

Routes.rb -- 在表单提交中使用了错误的URL,因此需要在Manager控制器中添加更新功能的路由。

  get 'users/edit' => 'manager#edit'
  post 'users/edit' => 'manager#edit'
  get 'users/update' => 'manager#update'
  post 'users/update' => 'manager#update'

Manager Controller -- 我不得不更改密码规则,这样我就可以在没有密码的情况下进行更新。概念相似,执行略有不同。

  def update
    @user = User.find(params[:user][:id])
    @companies = Company.all
    if params[:user][:password].blank? && params[:user][:password_confirmation].blank?
      params[:user].delete(:password)
      params[:user].delete(:password_confirmation)
    end
    if @user.update(user_params)
      flash[:notice] = "Successfully updated User."
      redirect_to root_path
    else
      render :action => 'edit'
    end
  end

已呈现 Form.html.erb - 使用新创建的 URL 路径更新。

<%= form_for(resource, as: @user, url: users_update_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>