ruby-on-rails - Rails 管理面板加载缓慢

标签 ruby-on-rails mongodb mongoid panel rails-admin

我目前正在使用 Mongoid ORM 进行 Rails 项目。我正在使用 Rails admin gem 在服务器端进行数据处理。 Rails 版本是 4.1.7,Ruby 是 2.1.5。问题是第一次加载管理面板时需要花费大量时间来加载。

我检查了 Rails 的日志。它似乎从“用户”表中查询每个用户,并且加载面板所花费的时间当然与用户表中的条目数成正比。

请帮助我加快面板的加载速度。如果需要任何其他信息,请询问。谢谢。

以下是rails日志的快照。

 MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('5656bd857261693fb10a0000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 10.0400ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('56ae41fe72616930bf030000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 0.4920ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('567d01277261695b8a000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 5.1850ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('568cad1a7261691ff2030000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 0.5010ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('561144df726169602d000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 2.4130ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('56ae487e72616930bf460000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 0.4500ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('567d01277261695b8a000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 5.1940ms
  MOPED: 127.0.0.1:27017 QUERY        database=yeloapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('56aa51f972616944d1300000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields
=nil runtime: 6.2580ms

能力.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
    #   user ||= User.new # guest user (not logged in)
    #   if user.admin?
    #     can :manage, :all
    #   else
    #     can :read, :all
    #   end
    #
    # The first argument to `can` is the action you are giving the user
    # permission to do.
    # If you pass :manage it will apply to every action. Other common actions
    # here are :read, :create, :update and :destroy.
    #
    # The second argument is the resource the user can perform the action on.
    # If you pass :all it will apply to every resource. Otherwise pass a Ruby
    # class of the resource.
    #
    # The third argument is an optional hash of conditions to further filter the
    # objects.
    # For example, here the user can only update published articles.
    #
    #   can :update, Article, :published => true
    #
    # See the wiki for details:
    # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
    if user &&  user.admin_type == 1
      can :access, :rails_admin       # only allow admin users to access Rails Admin
      can :dashboard  
      can :manage, :all
    end
    if user && user.is_admin? 
      can :access, :rails_admin       # only allow admin users to access Rails Admin
      can :dashboard  
      can :read, :all
      can :manage, :statistic
    end
  end
end

rails_admin.rb

RailsAdmin.config do |config|

  ### Popular gems integration
  ## == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)

  ## == Cancan ==
  #config.authorize_with :cancan

  ## == PaperTrail ==
  # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0

  ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end
end

编辑:我解决了问题(参见答案)。但我还是想知道为什么加载这么慢。感谢帮助。

最佳答案

所以我通过明确地将我想要展示的模型列入白名单来解决这个问题。所以我更改了rails_admin.rb 文件。

rails_admin.rb

RailsAdmin.config do |config|

  ### Popular gems integration
  ## == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)

  # this is the way to whitelist the models
  config.included_models = [write the name of models you want to show at rails panel]


  ## == Cancan ==
  config.authorize_with :cancan

  ## == PaperTrail ==
  # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0

  ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end
end

希望对其他人有帮助。

关于ruby-on-rails - Rails 管理面板加载缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36009904/

相关文章:

mysql - 寻求有关特定用例的 MongoDB 性能的见解

model-view-controller - 在 Rails 3 中添加按钮点击计数器

ruby-on-rails - 语法错误,意外的keyword_or范围

ruby-on-rails - Rails 5 + Shrine 多文件上传

ruby-on-rails - ROR 3.1 : Bundle update fails (eventmachine gem)

javascript - var/log/mongodb/mongod.log 从 node.js 打开和关闭连接的无限循环

ruby-on-rails - Mongoid - 具有引用数组的字段

ruby-on-rails - "Regular"Ruby gem 还是 Rails 引擎?

ruby-on-rails - 为 Rails 3.1.1 关闭详细的 sql/ActiveRecord

node.js - 在 MongoDB 中分区数据的最佳实践是什么?