首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同用户信息的扩展更新

不同用户信息的扩展更新
EN

Stack Overflow用户
提问于 2019-07-26 15:21:30
回答 2查看 370关注 0票数 0

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

我扩展了Devise,使我有一个用户/注册和用户/会话控制器。

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

我对Rails相当陌生,所以它可能很简单。我找到了解决方案,其中包括常规的设计安装,但没有扩展。我觉得问题可能出在我的路线上。

我也不能在这里创建新用户。

Routes.rb

代码语言:javascript
复制
  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

经理控制器

代码语言:javascript
复制
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

ManagerHelper

代码语言:javascript
复制
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

代码语言:javascript
复制
<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 %>

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

当前,当我在更改用户信息后单击update时,会得到此错误。

ActiveRecord::RecordNotFound在ManagerController#edit中找不到没有ID的用户,奇怪的是,当我看到用户信息时,url是:http://localhost:3000/users/edit?id=2,但是当我更新(并得到错误)时,url更改为:http://localhost:3000/users/edit.user

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-05 22:26:16

很多次尝试之后,我找到了一个解决方案。有很多错误,所以请容忍我。

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

代码语言:javascript
复制
  get 'users/edit' => 'manager#edit'
  post 'users/edit' => 'manager#edit'
  get 'users/update' => 'manager#update'
  post 'users/update' => 'manager#update'

经理控制器--我必须更改密码规则,这样才能在没有密码的情况下进行更新。概念相似,执行略有不同。

代码语言:javascript
复制
  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路径进行更新。

代码语言:javascript
复制
<%= form_for(resource, as: @user, url: users_update_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>
票数 0
EN

Stack Overflow用户

发布于 2019-07-28 09:29:56

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

代码语言:javascript
复制
:url => edit_user_registration_path(resource_name)

您不必在这里注册用户。如果管理员要更新路由文件中的信息。像这样的事情

代码语言:javascript
复制
namespace :admins do
    resources :users
end

然后使用下面创建的路径

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57222956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档