Two pages for the same resource - ActiveAdmin

前端 未结 2 532
轻奢々
轻奢々 2020-12-05 07:35

Currently I have User model, which is registered in user.rb as a new resource for ActiveAdmin. Generated page displays all users with scopes (

相关标签:
2条回答
  • 2020-12-05 07:50

    STI (Single table inheritance) can be used to create multiple "sub-resources" of the same table/parent model in Active admin

    1. Add a "type" column in user table as a string

    2. Add this to User model to mirror waiting field with type field

      after_commit {|i| update_attribute(:type, waiting ? "UserWaiting" : "UserNotWaiting" )}
      
    3. Create the new models UserWaiting and UserNotWaiting

      class UserWaiting < User
      end
      class UserNotWaiting < User
      end
      
    4. Create Active Admin resources

      ActiveAdmin.register UserWaiting do
      # ....
      end
      ActiveAdmin.register UserNotWaiting do
      # ....
      end
      
    5. You can run a first-time sync in console

      User.all.each {|user| user.save}
      

    ..............

    Another way could be to skip the type column (steps 1,2 and 5) and solve the rest with scopes.

    1. Step 3 and 4 above

    2. Then create the scopes

      #model/user.rb
      scope :waiting, where(:waiting => true)
      scope :not_waiting, where(:waiting => false)
      
    3. Scopes in Active Admin

      #admin/user.rb
      scope :waiting, :default => true
      
      #admin/user_not_waitings.rb
      scope :not_waiting, :default => true
      

    Just make sure the other scopes in these two pages are also filtered on waiting/not_waiting

    0 讨论(0)
  • 2020-12-05 07:56

    you could use a parameter to distinguish the cases and render different actions depending on the parameter:

    link_to users_path(:kind => 'waiting')
    

    and in the users_controller.rb

    def index
      if params[:kind]=='waiting'
        @users= Users.where(:waiting => true)
        render :action => 'waiting' and return
      else
        # do your other stuff
      end
    end
    

    then put your new, different page (partial) in app/views/users/waiting.html.erb

    If you want to use a different layout for this page add the layout parameter to render:

    render :action => 'waiting', :layout => 'other_layout' and return
    
    0 讨论(0)
提交回复
热议问题