【问题标题】:Update information of different user with extension of Devise使用 Devise 的扩展更新不同用户的信息
【发布时间】:2023-03-14 17:05:02
【问题描述】:

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

我扩展了 Devise,以便拥有用户/注册和用户/会话控制器。

管理员用户可以访问 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

经理/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 找不到没有 ID 的用户 奇怪的是,当我看到用户信息时,网址是:http://localhost:3000/users/edit?id=2 但是当我更新(并得到错误)时,网址更改为:http://localhost:3000/users/edit.user

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

【问题讨论】:

    标签: ruby-on-rails devise cancancan


    【解决方案1】:

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

    :url => edit_user_registration_path(resource_name)
    

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

    namespace :admins do
        resources :users
    end
    

    然后使用由此创建的路径

    【讨论】:

    • 谢谢 - 我认为这带来了比我想要的更多的问题。当我尝试实现它时,我必须更新很多我的路线。除非重做所有这些有显着的好处,否则我会喜欢只允许我编辑用户的路线。
    • 获得了一些 IRL 帮助并找到了解决方案。你是对的 - 这是我的 URL 的问题,但我无法弄清楚你建议的路线更改将如何解决它。感谢您的帮助。我在下面发布了我的解决方案。
    【解决方案2】:

    经过多次尝试,我找到了解决方案。有很多错误,所以请多多包涵。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2016-10-03
      相关资源
      最近更新 更多