Note that there are some explanatory texts on larger screens.

plurals
  1. POActive admin user new page got error
    primarykey
    data
    text
    <p>I have two types of admin users</p> <ol> <li>Super Admin</li> <li>Institution Admin</li> </ol> <p>Using cancan for following things.</p> <p>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.</p> <p>Institution admin can create user only related to the institution and can see only users related to that institution.</p> <p>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.</p> <pre><code>ActiveRecord::HasManyThroughNestedAssociationsAreReadonly in Admin::UsersController#new Cannot modify association 'AdminUser#users' because it goes through more than one other association. </code></pre> <p>models/admin_user.rb</p> <pre><code>class AdminUser &lt; ActiveRecord::Base belongs_to :institution has_many :profiles, :through =&gt; :institution has_many :users, :through =&gt; :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 </code></pre> <p>models/ability.rb</p> <pre><code>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 =&gt; {:institution_id =&gt; current_admin_user.institution_id} can :manage, InterestType, :institution_id =&gt; current_admin_user.institution_id end end end </code></pre> <p>controllers/users_controller.rb</p> <pre><code> class UsersController &lt; ApplicationController layout "home" skip_before_filter :require_login, :only =&gt; [: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 =&gt; "Please check email." else render :new, :alert =&gt; @user.errors end end end </code></pre> <p>admin/admin_users.rb</p> <pre><code>ActiveAdmin.register AdminUser do menu :if =&gt; 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 =&gt; :select, :include_blank =&gt; false, :collection =&gt; Hash[ "Institution Administrator", "institution_admin", "Administrator", "super_admin"] f.input :institution_id, :as =&gt; :select, :include_blank =&gt; false, :collection =&gt; 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 </code></pre> <p>admin/users.rb</p> <pre><code>ActiveAdmin.register User do menu :if =&gt; proc{ can?(:manage, User) } controller.authorize_resource scope_to :current_admin_user, :association_method =&gt; :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 =&gt; [: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 =&gt; 1995..2020, :selected =&gt; Time.now.year if current_admin_user.role == "super_admin" profile_form.input :institution_id, :as =&gt; :select, :include_blank =&gt; false, :collection =&gt; Institution.all.map{ |ins| [ins.name, ins.id] } elsif current_admin_user.role == "institution_admin" profile_form.input :institution_id, :as =&gt; :hidden, :value =&gt; current_admin_user.institution_id end end f.buttons end end </code></pre> <blockquote> <p>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)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload