Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 活动管理员用户新页面出现错误_Ruby On Rails 3_Activeadmin - Fatal编程技术网

Ruby on rails 3 活动管理员用户新页面出现错误

Ruby on rails 3 活动管理员用户新页面出现错误,ruby-on-rails-3,activeadmin,Ruby On Rails 3,Activeadmin,我有两种类型的管理员用户 超级管理员 机构管理 使用cancan进行以下操作 超级管理员可以创建机构管理员/另一个超级管理员以及与机构相关的普通用户,并可以管理所有其他事情,如兴趣类型、目标等。 超级管理员还可以查看系统中创建的所有用户 机构管理员只能创建与该机构相关的用户,并且只能查看与该机构相关的用户 所以一切正常,除非有一件事。当我使用institutional admin登录并进入页面创建新用户时,会显示以下错误 ActiveRecord::HasManyThroughNestedAss

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

  • 超级管理员
  • 机构管理
  • 使用cancan进行以下操作

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

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

    所以一切正常,除非有一件事。当我使用institutional admin登录并进入页面创建新用户时,会显示以下错误

    ActiveRecord::HasManyThroughNestedAssociationsAreReadonly in Admin::UsersController#new 
    Cannot modify association 'AdminUser#users' because it goes through more than one other association.
    
    型号/管理用户.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
    
    控制器/用户\u 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/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我可以从机构用户创建新用户,但在索引上 页面我可以看到所有用户(而不仅仅是来自 (机构)


    您可以通过将
    所有用户
    中的
    用户
    行修改为:

    User.joins(:profile => :institution).where(["#{Institution.table_name}.id = ?", institution.id])
    
    这不会在新用户上设置配置文件,因此您仍然需要以与分配机构相同的方式在表单中分配配置文件。您还需要确保档案和机构之间存在双向关联


    出现错误的原因是ActiveRecord不允许您通过关联创建嵌套多个记录的新记录。我们正在通过获得相同关联的结果来解决这一问题,但没有经过多次测试。该查询相当于说“获取属于某个配置文件的所有用户,而该配置文件又是某个机构的一部分,其中该机构的id与当前用户的机构id相同”.

    也许您可以向我们展示UsersController新方法。用控制器代码更新了我的问题我知道问题是与管理员用户和用户之间的关系有关,但无法解决问题。您可以向我们展示您的用户和管理员用户的活动\u管理员控制器吗?你好@jon,在AdminName中添加了admin_users.rb和users.rb的代码,但我不知道所有表名部分的条件在哪里,以及从哪里可以获得机构id,请您解释一下,这样我就可以向回答者奖励奖金了OK,谢谢您的解释,但是我以前从来没有在rails中使用过类似model.table_name的东西,它的用途是什么?这是获取模型表名的安全方法。在您的情况下,最有可能是机构,但这并不总是一个保证,特别是如果您使用的是遗留数据库。
    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
    
    User.joins(:profile => :institution).where(["#{Institution.table_name}.id = ?", institution.id])