RailsAdmin从多个引擎配置

时间:2013-05-24 03:15:53

标签: ruby-on-rails rails-admin

我有rails app,它有多个引擎userscompanies等。

#main_app: /config/routes.rb
Rails.application.routes.draw do
  mount RailsAdmin::Engine, at: 'admin', as: 'rails_admin'
  mount Users::Engine, at: "users", as: "users"
  mount Companies::Engine, at: "companies", as: "companies"
  ...
end

我想从main_app和每个引擎制作配置rails_admin

#main_app: /config/initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.authorize_with :cancan
  ...
end

在这种情况下,所有工作都很好:

$ rake routes
...
Routes for RailsAdmin::Engine:
    dashboard GET         /                                      rails_admin/main#dashboard
        index GET|POST    /:model_name(.:format)                 rails_admin/main#index
          new GET|POST    /:model_name/new(.:format)             rails_admin/main#new
       export GET|POST    /:model_name/export(.:format)          rails_admin/main#export
  bulk_delete POST|DELETE /:model_name/bulk_delete(.:format)     rails_admin/main#bulk_delete
history_index GET         /:model_name/history(.:format)         rails_admin/main#history_index
  bulk_action POST        /:model_name/bulk_action(.:format)     rails_admin/main#bulk_action
         show GET         /:model_name/:id(.:format)             rails_admin/main#show
         edit GET|PUT     /:model_name/:id/edit(.:format)        rails_admin/main#edit
       delete GET|DELETE  /:model_name/:id/delete(.:format)      rails_admin/main#delete
 history_show GET         /:model_name/:id/history(.:format)     rails_admin/main#history_show
  show_in_app GET         /:model_name/:id/show_in_app(.:format) rails_admin/main#show_in_app

但是当我尝试从引擎发送其他配置时:

#users_engine: /config/initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.excluded_models  << "Users::Phones::ConfirmAttempt"

  config.actions do
    new do
      except ["Users::User"]
    end
  end
  ...
end

我的路线中有一些奇怪的东西,我无法通过浏览器访问rails_admin

$ rake routes
...
Routes for RailsAdmin::Engine:
        new GET|POST /:model_name/new(.:format)         rails_admin/main#new
bulk_action POST     /:model_name/bulk_action(.:format) rails_admin/main#bulk_action

有人可以解释一下,如何解决这个问题。或者mb另一种方式我如何能够从每个引擎向rails_admin发送配置?

2 个答案:

答案 0 :(得分:3)

您无法执行此操作,因为RailsAdmin.config会清除以前的设置。作为一种解决方法,您可以为主应用程序中的所有引擎配置RailsAdmin。

答案 1 :(得分:1)

Rails管理员中的

config.actions会覆盖所有默认操作。你必须在这样的配置中再次定义它们:

users_engine: /config/initializers/rails_admin.rb
RailsAdmin.config do |config|

  config.actions do
    # root actions
    dashboard                     # mandatory
    # collection actions 
    index                         # mandatory
    new do
      except ["Users::User"]
    end
    export
    history_index
    bulk_delete
    # member actions
    show
    edit
    delete
    history_show
    show_in_app
  end
  ...
end

但是,这也会覆盖主应用程序或其他引擎中的任何配置。