活动的管理员用户新页面出现错误 [英] Active admin user new page got error

查看:93
本文介绍了活动的管理员用户新页面出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种类型的管理员用户

I have two types of admin users


  1. 超级管理员

  2. 机构管理员

使用cancan进行以下操作。

Using cancan for following things.

超级管理员可以创建机构管理员/另一个超级管理员管理员以及与该机构有关的普通用户,并且可以管理所有其他事物,例如兴趣类型,目标...等。
超级管理员还可以查看系统中创建的所有用户。

Super admin can create Institution admin / another super admin as well as normal users related to the institution, and can manage all the other things like interest types, goals....etc. Also super admin can see all the users created in the system.

机构管理员可以创建仅与机构相关的用户,并且只能查看与该机构相关的用户

Institution admin can create user only related to the institution and can see only users related to that institution.

因此,除非有1件事,否则一切正常。当我以机构管理员身份登录并转到页面上以创建新用户时,它向我显示以下错误。

So everything working fine unless 1 thing. When i logged in with institutional admin and go on the page to create a new user it shows me following error.

ActiveRecord::HasManyThroughNestedAssociationsAreReadonly in Admin::UsersController#new 
Cannot modify association 'AdminUser#users' because it goes through more than one other association.

models / admin_user.rb

models/admin_user.rb

class AdminUser < ActiveRecord::Base

  belongs_to :institution
  has_many :profiles, :through => :institution
  has_many :users, :through => :profiles

  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable

  def password_required?
    new_record? ? false : super
  end

  def all_users
    if role == "super_admin"
     User.unscoped
    else
     #may be below line of code has issue.
     users       
    end
  end
 end

模型/ability.rb

models/ability.rb

class Ability
  include CanCan::Ability

  def initialize(current_admin_user)
    # Define abilities for the passed in user here. For example:

    current_admin_user ||= AdminUser.new # guest user (not logged in)

    case current_admin_user.role
    when "super_admin"  
      can :manage, :all  
    when "institution_admin"
      can :manage, User, :profile => {:institution_id => current_admin_user.institution_id}
      can :manage, InterestType, :institution_id => current_admin_user.institution_id 
    end
    end
   end

controllers / users_controller .rb

controllers/users_controller.rb

 class UsersController < ApplicationController
      layout "home"
      skip_before_filter :require_login, :only => [:new, :create, :activate]

      def new
        @user = User.new
        @user.build_profile
      end

      def create
        @user = User.new(params[:user])
        if @user.save
           redirect_to home_path, :notice => "Please check email."
        else
           render :new, :alert => @user.errors
        end
      end
    end

admin / admin_users.rb

admin/admin_users.rb

ActiveAdmin.register AdminUser do

  menu :if => proc{ can?(:manage, AdminUser) }        controller.authorize_resource 

  index do
    column :email
    column ("Role") {|admin_user| admin_user.role == "super_admin" ? "Administrator" : "Institution Administrator" }  
    column ("Instituion") { |admin_user| admin_user.institution.name unless admin_user.institution_id.nil? }
    column :current_sign_in_at
    column :last_sign_in_at
    column :sign_in_count
    default_actions   end

  #...   form do |f|
    f.inputs "Admin Details" do
      f.input :email
      f.input :role, :as => :select, :include_blank => false, :collection => Hash[ "Institution Administrator", "institution_admin", "Administrator", "super_admin"]
      f.input :institution_id, :as => :select, :include_blank => false, :collection => Institution.all.map{ |ins| [ins.name, ins.id] }
    end
      f.buttons   end

  after_create { |admin| admin.send_reset_password_instructions unless admin.email.blank? and admin.institution_id.blank? }
    def password_required?
    new_record? ? false : super   end

end

admin / users.rb

admin/users.rb

ActiveAdmin.register User do

  menu :if => proc{ can?(:manage, User) }     
  controller.authorize_resource 
  scope_to :current_admin_user, :association_method => :all_users

  index do

    column :username
    column :email
    column ("Instituion") { |user| user.profile.institution.name }
    column :activation_state
    column("Name" ) {|user| user.profile.users_firstname + " " + user.profile.users_lastname}
    column :created_at
  end

  form do |f|

    f.inputs "User Details" do
      f.input :username
      f.input :email
      if f.object.id.nil?
          f.input :password
          f.input :password_confirmation
      end
    end

    f.inputs "Profile Details", :for => [:profile, f.object.profile || Profile.new] do |profile_form|
      profile_form.input :users_firstname
      profile_form.input :users_lastname
      profile_form.input :users_telephone
      profile_form.input :class_year, :collection => 1995..2020,  :selected => Time.now.year
      if current_admin_user.role == "super_admin"
        profile_form.input :institution_id, :as => :select, :include_blank => false, :collection => Institution.all.map{ |ins| [ins.name, ins.id] }
      elsif current_admin_user.role == "institution_admin"
        profile_form.input :institution_id, :as => :hidden, :value => current_admin_user.institution_id
      end
    end

    f.buttons
  end


end




注意:当我将admin_users.rb中的def all_users从用户编辑为
User.scoped我可以从机构用户创建新用户,但在索引
页面上我可以看到所有用户(而不是仅来自
机构的用户)

Note: When i edit def all_users in admin_users.rb from users to User.scoped i can create new user from institution user but on index page i can see all the users(instead of only the users from the institution)


推荐答案

您可以通过修改 all_users <中的 users 行来解决错误。 / code>到:

You can fix the error by modifying the users line in all_users to:

User.joins(:profile => :institution).where(["#{Institution.table_name}.id = ?", institution.id])

这不会设置配置文件在新用户上,因此您仍然需要以与分配机构相同的方式来分配它。您还需要确保Profile和Institution之间存在双向关联。

This wont set the profile on the new user, so you will still need to assign that in the form the same way you assign institution. You will also need to make sure the bi-directional associations exist between Profile and Institution.

出现错误的原因是因为ActiveRecord不允许您创建新记录通过关联脱离嵌套的has_many。我们正在通过获取相同关联的结果而不通过has_many来解决此问题。该查询等同于说让我成为个人资料一部分的所有用户,而个人资料又是机构的一部分,其中机构的ID与当前用户的机构的ID相同。

The reason you got the error is because ActiveRecord will not allow you to create new records off of a nested has_many through association. We are are working around this by getting the results of the same association but without going through a has_many. The query is the equivalent of saying "get me all users who are part of a profile, which is in turn part of an institution, where the institution's id is the same as the current user's institution's id".

这篇关于活动的管理员用户新页面出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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