4

我按照说明安装了 Rails_Admin,并且也在使用 Devise。当我尝试导航到“/admin”时,出现以下错误:

Routing Error

No route matches {:controller=>"home"}

这是我的路线.rb:

Ot::Application.routes.draw do

  resources :badgeships
  resources :badges
  resources :profiles
  resources :universities
  resources :degrees
  resources :jobs
  resources :resources
  resources :communities
  resources :networks

  resources :topics do
        resources :posts
  end

  get 'topics/tag/:tag', to: 'topics#index', as: :topic_tag

  get 'resources/tag/:tag', to: 'resources#index', as: :resource_tag

  root :to => 'home#index'

  devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}

  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

end

这是我的 rails_admin.rb 初始化程序:

RailsAdmin.config do |config|
  config.main_app_name = ["Cool app", "BackOffice"]
  # or somethig more dynamic
  config.main_app_name = Proc.new { |controller| [ "Cool app", "BackOffice - #{controller.params[:action].try(:titleize)}" ] }
end

关于如何让 rails_admin 正常工作的任何想法?

更新:这是我的耙子路线:

[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
              badgeships GET    /badgeships(.:format)                      badgeships#index
                         POST   /badgeships(.:format)                      badgeships#create
           new_badgeship GET    /badgeships/new(.:format)                  badgeships#new
          edit_badgeship GET    /badgeships/:id/edit(.:format)             badgeships#edit
               badgeship GET    /badgeships/:id(.:format)                  badgeships#show
                         PUT    /badgeships/:id(.:format)                  badgeships#update
                         DELETE /badgeships/:id(.:format)                  badgeships#destroy
                  badges GET    /badges(.:format)                          badges#index
                         POST   /badges(.:format)                          badges#create
               new_badge GET    /badges/new(.:format)                      badges#new
              edit_badge GET    /badges/:id/edit(.:format)                 badges#edit
                   badge GET    /badges/:id(.:format)                      badges#show
                         PUT    /badges/:id(.:format)                      badges#update
                         DELETE /badges/:id(.:format)                      badges#destroy
                profiles GET    /profiles(.:format)                        profiles#index
                         POST   /profiles(.:format)                        profiles#create
             new_profile GET    /profiles/new(.:format)                    profiles#new
            edit_profile GET    /profiles/:id/edit(.:format)               profiles#edit
                 profile GET    /profiles/:id(.:format)                    profiles#show
                         PUT    /profiles/:id(.:format)                    profiles#update
                         DELETE /profiles/:id(.:format)                    profiles#destroy
            universities GET    /universities(.:format)                    universities#index
                         POST   /universities(.:format)                    universities#create
          new_university GET    /universities/new(.:format)                universities#new
         edit_university GET    /universities/:id/edit(.:format)           universities#edit
              university GET    /universities/:id(.:format)                universities#show
                         PUT    /universities/:id(.:format)                universities#update
                         DELETE /universities/:id(.:format)                universities#destroy
                 degrees GET    /degrees(.:format)                         degrees#index
                         POST   /degrees(.:format)                         degrees#create
              new_degree GET    /degrees/new(.:format)                     degrees#new
             edit_degree GET    /degrees/:id/edit(.:format)                degrees#edit
                  degree GET    /degrees/:id(.:format)                     degrees#show
                         PUT    /degrees/:id(.:format)                     degrees#update
                         DELETE /degrees/:id(.:format)                     degrees#destroy
                    jobs GET    /jobs(.:format)                            jobs#index
                         POST   /jobs(.:format)                            jobs#create
                 new_job GET    /jobs/new(.:format)                        jobs#new
                edit_job GET    /jobs/:id/edit(.:format)                   jobs#edit
                     job GET    /jobs/:id(.:format)                        jobs#show
                         PUT    /jobs/:id(.:format)                        jobs#update
                         DELETE /jobs/:id(.:format)                        jobs#destroy
               resources GET    /resources(.:format)                       resources#index
                         POST   /resources(.:format)                       resources#create
            new_resource GET    /resources/new(.:format)                   resources#new
           edit_resource GET    /resources/:id/edit(.:format)              resources#edit
                resource GET    /resources/:id(.:format)                   resources#show
                         PUT    /resources/:id(.:format)                   resources#update
                         DELETE /resources/:id(.:format)                   resources#destroy
             communities GET    /communities(.:format)                     communities#index
                         POST   /communities(.:format)                     communities#create
           new_community GET    /communities/new(.:format)                 communities#new
          edit_community GET    /communities/:id/edit(.:format)            communities#edit
               community GET    /communities/:id(.:format)                 communities#show
                         PUT    /communities/:id(.:format)                 communities#update
                         DELETE /communities/:id(.:format)                 communities#destroy
                networks GET    /networks(.:format)                        networks#index
                         POST   /networks(.:format)                        networks#create
             new_network GET    /networks/new(.:format)                    networks#new
            edit_network GET    /networks/:id/edit(.:format)               networks#edit
                 network GET    /networks/:id(.:format)                    networks#show
                         PUT    /networks/:id(.:format)                    networks#update
                         DELETE /networks/:id(.:format)                    networks#destroy
             topic_posts GET    /topics/:topic_id/posts(.:format)          posts#index
                         POST   /topics/:topic_id/posts(.:format)          posts#create
          new_topic_post GET    /topics/:topic_id/posts/new(.:format)      posts#new
         edit_topic_post GET    /topics/:topic_id/posts/:id/edit(.:format) posts#edit
              topic_post GET    /topics/:topic_id/posts/:id(.:format)      posts#show
                         PUT    /topics/:topic_id/posts/:id(.:format)      posts#update
                         DELETE /topics/:topic_id/posts/:id(.:format)      posts#destroy
                  topics GET    /topics(.:format)                          topics#index
                         POST   /topics(.:format)                          topics#create
               new_topic GET    /topics/new(.:format)                      topics#new
              edit_topic GET    /topics/:id/edit(.:format)                 topics#edit
                   topic GET    /topics/:id(.:format)                      topics#show
                         PUT    /topics/:id(.:format)                      topics#update
                         DELETE /topics/:id(.:format)                      topics#destroy
               topic_tag GET    /topics/tag/:tag(.:format)                 topics#index
            resource_tag GET    /resources/tag/:tag(.:format)              resources#index
                    root        /                                          home#index
        new_user_session GET    /users/login(.:format)                     devise/sessions#new
            user_session POST   /users/login(.:format)                     devise/sessions#create
    destroy_user_session DELETE /users/logout(.:format)                    devise/sessions#destroy
 user_omniauth_authorize        /users/auth/:provider(.:format)            devise/omniauth_callbacks#passthru {:provider=>/facebook/}
  user_omniauth_callback        /users/auth/:action/callback(.:format)     devise/omniauth_callbacks#(?-mix:facebook)
           user_password POST   /users/password(.:format)                  devise/passwords#create
       new_user_password GET    /users/password/new(.:format)              devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)             devise/passwords#edit
                         PUT    /users/password(.:format)                  devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                    devise/registrations#cancel
       user_registration POST   /users(.:format)                           devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                   devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                      devise/registrations#edit
                         PUT    /users(.:format)                           devise/registrations#update
                         DELETE /users(.:format)                           devise/registrations#destroy
             rails_admin        /admin                                     RailsAdmin::Engine

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

这是我安装 rails_admin 的方法:

    rails g rails_admin:install
        SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
        This poses a security threat. It is strongly recommended that you
        provide a secret to prevent exploits that may be possible from crafted
        cookies. This will not be supported in future versions of Rack, and
        future versions will even invalidate your existing user cookies.

        Called from: /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.9/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.

           -  Hello, RailsAdmin installer will help you set things up!
           -  I need to work with Devise, let's look at a few things first:
           -  Checking for a current installation of devise...
           -  Found it!
           -  Looks like you've already installed it, good!
           ?  Where do you want to mount rails_admin? Press <enter> for [admin] > manage
        gsub  config/routes.rb
       route  mount RailsAdmin::Engine => '/manage', :as => 'rails_admin'
           -  And you already set it up, good! We just need to know about your user model name...
           -  We found 'user' (should be one of 'user', 'admin', etc.)
           ?  Correct Devise model name if needed. Press <enter> for [user] > User
           -  Now setting up devise with user model name 'User':
    generate  devise
        SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
        This poses a security threat. It is strongly recommended that you
        provide a secret to prevent exploits that may be possible from crafted
        cookies. This will not be supported in future versions of Rack, and
        future versions will even invalidate your existing user cookies.

        Called from: /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.9/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.

      invoke  active_record
      create    db/migrate/20130214195616_add_devise_to_users.rb
      insert    app/models/user.rb
       route  devise_for :users
           -  Now you'll need an initializer...
           -  You already have a config file. You're updating, heh? I'm generating a new 'rails_admin.rb.example' that you can review.
      create  config/initializers/rails_admin.rb.example
/Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activerecord-3.2.9/lib/active_record/reflection.rb:385:in `block in source_reflection': undefined method `klass' for nil:NilClass (NoMethodError)
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activerecord-3.2.9/lib/active_record/reflection.rb:385:in `collect'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activerecord-3.2.9/lib/active_record/reflection.rb:385:in `source_reflection'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activerecord-3.2.9/lib/active_record/reflection.rb:375:in `rescue in foreign_key'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activerecord-3.2.9/lib/active_record/reflection.rb:373:in `foreign_key'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/adapters/active_record.rb:292:in `association_foreign_key_lookup'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/adapters/active_record.rb:72:in `block in associations'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/adapters/active_record.rb:65:in `map'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/adapters/active_record.rb:65:in `associations'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields/factories/association.rb:6:in `block in <top (required)>'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:54:in `call'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:54:in `block (2 levels) in factory'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:54:in `each'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:54:in `find'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:54:in `block in factory'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:50:in `each'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/fields.rb:50:in `factory'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/has_fields.rb:130:in `_fields'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/has_fields.rb:128:in `_fields'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/has_fields.rb:108:in `all_fields'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/rails_admin/config/has_fields.rb:83:in `fields'
    from (erb):64:in `block in template'
    from (erb):53:in `map'
    from (erb):53:in `template'
    from /Users/iyerushalmi/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/erb.rb:838:in `eval'
    from /Users/iyerushalmi/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/erb.rb:838:in `result'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/file_manipulation.rb:117:in `block in template'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:54:in `call'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:54:in `render'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:63:in `block (2 levels) in invoke!'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:63:in `open'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:63:in `block in invoke!'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/empty_directory.rb:133:in `call'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/empty_directory.rb:133:in `invoke_with_conflict_check'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:61:in `invoke!'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions.rb:95:in `action'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/create_file.rb:26:in `create_file'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/actions/file_manipulation.rb:116:in `template'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/rails_admin-0.4.4/lib/generators/rails_admin/install_generator.rb:66:in `install'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/task.rb:27:in `run'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/invocation.rb:120:in `invoke_task'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/invocation.rb:126:in `block in invoke_all'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/invocation.rb:126:in `each'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/invocation.rb:126:in `map'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/invocation.rb:126:in `invoke_all'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/group.rb:238:in `dispatch'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/thor-0.17.0/lib/thor/base.rb:434:in `start'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.9/lib/rails/generators.rb:171:in `invoke'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.9/lib/rails/commands/generate.rb:12:in `<top (required)>'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require'
    from /Users/iyerushalmi/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.9/lib/rails/commands.rb:29:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
4

5 回答 5

6

编辑:找到修复。请参阅此 SO 讨论

进入您的主 ApplicationController 并更改

rescue_from CanCan::AccessDenied do |e|
  redirect_to root_path, alert: e.message
end

rescue_from CanCan::AccessDenied do |e|
  redirect_to '/', alert: e.message
end

给它任何符号路径名将导致 RoutingException,因为 Rails 正在错误的路由表中查找名称。指定字面根路径绕过查找,并让主应用程序的 ApplicationController 第一次处理它。这就是你想要的。

原帖如下hr


我遇到了类似的错误,如果非管理员用户访问我的“/admin”路由,那么 ActiveAdmin+CanCan 会尝试将他踢出去。(顺便说一下,通过添加 Gem,您可以获得更清晰的错误报告better_errors。)我得到了异常报告,并且我可以在发生路由失败的地方在局部变量中看到正确的错误消息。

由于 better_errors 的“live shell”,我还可以看到生成 RoutingError 的 ActionController 是RailsAdmin::MainController. 这表明我们需要弄清楚如何告诉 RailsAdmin 重定向到应用程序根路径,而不是引擎根路径。

于 2013-03-19T07:12:14.647 回答
1

您的路由不起作用的原因是因为您注入了

mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

routes.rb文件的底部。了解路线从上到下优先。当一个 url 被路由时,它将从列表的顶部开始并向下。一旦发现匹配的路由,Rails 就会将其发送给适当的控制器和动作。

一般来说,我发现将第三方 API 移到 API 列表顶部是最佳实践。如果最终出现冲突问题,我可以更改自己创建的路线。处理起来容易得多。

所以将 Devise 和 RailsAdmin 路由移到顶部并告诉我们你得到了什么。

于 2013-02-13T18:23:03.930 回答
0

我遇到了同样的问题,但只有当我使用我的用户模型登录时才会出现。如果我没有登录,它会重定向到我的登录页面。

更新

发生在我身上的是我的用户模型不允许:manage仪表板访问的资源。

一旦我包含了这个授权,管理面板就工作了。

于 2013-04-11T14:22:50.520 回答
0

尝试将设计和管理路由移动到路由文件的顶部。

于 2013-02-13T12:37:54.383 回答
0

如文档中所述,在 中使用main_approutes.rb引用主应用程序路由。

class ApplicationController < ActionController::Base
  rescue_from CanCan::AccessDenied do |exception|
    redirect_to main_app.root_path, :alert => exception.message
  end
end
于 2016-05-20T15:57:16.023 回答