1

我有 rails 应用程序,它有多个引擎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

我想rails_admin从 main_app 以及每个引擎进行配置。

#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从每个引擎发送配置?

4

2 回答 2

3

您不能这样做,因为 RailsAdmin.config 会清除之前的设置。作为一种解决方法,您可以为主应用程序中的所有引擎配置 RailsAdmin。

于 2013-05-30T18:21:33.640 回答
1

config.actions在 Rails Admin 中覆盖所有默认操作。您必须在配置中再次定义它们,如下所示:

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

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

于 2013-05-26T10:27:10.627 回答