我正在使用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时,我收到“No route matches”/ admin“”错误。我注意到我的routes.rb只有注释行。我做错了吗?
答案 0 :(得分:1)
您尚未为管理员添加路线,为什么所有路线都已注释掉。
如果admin是资源,请添加此行
resources :admin
同样在您的控制器中,您将需要一个索引方法和索引视图文件,因为http://localhost:3000/admin
将带您到那里
答案 1 :(得分:0)
尝试从此行中删除评论:
match ':controller(/:action(/:id(.:format)))'
答案 2 :(得分:0)
注意:此答案适用于满足上述要点而您仍然看到此错误的情况。是Rails
3.2
和Ruby 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
并重新启动了服务器,问题消失了。