【问题标题】:No route matches "/admin" error没有路由匹配“/admin”错误
【发布时间】:2020-06-04 17:52:53
【问题描述】:

我正在用 Rails 编写我的第一个应用程序,这就是我所做的

C:\Personal\rails\demo>ruby -v
    ruby 1.9.2p136 (2010-12-25) [i386-mingw32]

C:\Personal\rails\demo>rails -v
    Rails 3.0.5

C:\Personal\rails\demo>rails generate model book
      invoke  active_record
      create    db/migrate/20110325190010_create_books.rb
      create    app/models/book.rb
      invoke    test_unit
      create      test/unit/book_test.rb
      create      test/fixtures/books.yml

C:\Personal\rails\demo>rake db:migrate
(in C:/Personal/rails/demo)
==  CreateBooks: migrating ====================================================
-- create_table(:books)
   -> 0.0000s
==  CreateBooks: migrated (0.0000s) ===========================================


C:\Personal\rails\demo>rails generate controller admin
      create  app/controllers/admin_controller.rb
      invoke  erb
      create    app/views/admin
      invoke  test_unit
      create    test/functional/admin_controller_test.rb
      invoke  helper
      create    app/helpers/admin_helper.rb
      invoke    test_unit
      create      test/unit/helpers/admin_helper_test.rb

然后我编辑了 admin_controller.rb 如下:

class AdminController < ApplicationController
    scaffold :book
end

这是 routes.rb 文件

Demo::Application.routes.draw do
  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => "welcome#index"

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id(.:format)))'
end

但是,当我转到 http://localhost:3000/admin 时,我收到“没有路由匹配“/admin””错误。我注意到我的 routes.rb 只有注释行。我是不是做错了什么?

【问题讨论】:

  • 你能发布你的 routes.rb 文件吗?
  • 感谢收看。我已将 routes.rb 文件的内容添加到问题中。

标签: ruby-on-rails-3


【解决方案1】:

你还没有为管理员添加路由,为什么你的所有路由都被注释掉了。

如果管理员是一个资源添加这一行

resources :admin

在您的控制器中,您还需要一个索引方法和索引视图文件,因为http://localhost:3000/admin 会带您到那里

【讨论】:

  • 我按照“使用 Rails 进行敏捷 Web 开发”一书中的说明进行操作。让我试试看。
  • 嗯,这给了我一个错误“AdminController:Class 的未定义方法 `scaffold'” 上面提到的书适用于 rails 2,看起来其中的步骤不再适用于 3.0
  • 移除那一行脚手架 :book
  • 在哪里添加这些资源:admin?我是新来的轨道。我也遇到这个错误
【解决方案2】:

尝试从这一行删除评论:

match ':controller(/:action(/:id(.:format)))'

【讨论】:

    【解决方案3】:

    注意:此答案适用于满足上述几点但您仍然看到此错误的情况。是 Rails 3.2Ruby 2.3.8


    问题定义:

    我使用管理员凭据成功登录。当页面登陆/admin 时,我看到了No route matches [GET] "/admin" 错误页面。我到处碰头,用了我所有的知识。此外,生成的路由和grep,但没有发布admin 路由。

    $ rake routes | grep admin
    

    但只发布了设计相关的管理路线。然后我阅读了关于我后来注意到的路线的警告,就像

    ActiveAdmin: ActiveAdmin::DatabaseHitDuringLoad: Your file, app/admin/account_tags.rb (line 4), 
    caused a database error while Active Admin was loading. This is most common 
    when your database is missing or doesn't have the latest migrations applied. 
    To prevent this error, move the code to a place where it will only be run 
    when a page is rendered. One solution can be, to wrap the query in a Proc.
    Original error message: PG::UndefinedTable: ERROR:  relation "account_tags" does not exist
    

    解决方案

    我运行rake db:migrate 并重新启动服务器,问题就消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多