Rails动作控制器路由错误

时间:2013-06-11 09:05:47

标签: ruby-on-rails ruby routes

我尝试在我的网站上添加管理区域。所以我添加了一个包含控制器“posts”和“category”的命名空间。 在routes.rb文件中,我添加了

namespace :admin do
 resources :posts , :categories
 match '/' => 'posts#index', :via => :get
end

但是使用网址:

  

myurl.com/admin

我有错误:

  

没有路线匹配{:controller =>“admin / site”,:action =>“home”,:locale =>:fr}

似乎调用另一条路线并覆盖我添加的路线

但在rake路线我有:

  

admin GET /admin(.:format)admin / posts #index

这里有完整的佣金路线:

    faq GET    (/:locale)/faq(.:format)                    site#faq {:locale=>/[a-zA-Z]{2}/}
              rules GET    (/:locale)/rules(.:format)                  site#rules {:locale=>/[a-zA-Z]{2}/}
     advanced_rules GET    (/:locale)/advanced_rules(.:format)         site#advanced_rules {:locale=>/[a-zA-Z]{2}/}
           play_now GET    (/:locale)/play_now(.:format)               play#play {:locale=>/[a-zA-Z]{2}/}
               play GET    (/:locale)/play(.:format)                   site#play {:locale=>/[a-zA-Z]{2}/}
               home GET    (/:locale)/home(.:format)                   site#home {:locale=>/[a-zA-Z]{2}/}
                    GET    (/:locale)/qrcode(/:code)(.:format)         site#qrcode {:locale=>/[a-zA-Z]{2}/}
    forgot_password GET    (/:locale)/forgot_password/:id(.:format)    passwords#forgot_password {:locale=>/[a-zA-Z]{2}/}
   android_redirect GET    (/:locale)/android_redirect(.:format)       android#index {:locale=>/[a-zA-Z]{2}/}
 general_conditions GET    (/:locale)/general_conditions(.:format)     site#general_conditions {:locale=>/[a-zA-Z]{2}/}
            support GET    (/:locale)/support(.:format)                site#support {:locale=>/[a-zA-Z]{2}/}
                    GET    (/:locale)/invitation(/:source)(.:format)   invitation#redirection {:locale=>/[a-zA-Z]{2}/}
      release_notes GET    (/:locale)/release_notes(.:format)          site#release_notes {:locale=>/[a-zA-Z]{2}/}
       release_note GET    (/:locale)/release_notes/:version(.:format) release_notes#show {:locale=>/[a-zA-Z]{2}/}
               root GET    /(:locale)(.:format)                        site#home {:locale=>/[a-zA-Z]{2}/}
        admin_posts GET    /admin/posts(.:format)                      admin/posts#index
                    POST   /admin/posts(.:format)                      admin/posts#create
     new_admin_post GET    /admin/posts/new(.:format)                  admin/posts#new
    edit_admin_post GET    /admin/posts/:id/edit(.:format)             admin/posts#edit
         admin_post GET    /admin/posts/:id(.:format)                  admin/posts#show
                    PUT    /admin/posts/:id(.:format)                  admin/posts#update
                    DELETE /admin/posts/:id(.:format)                  admin/posts#destroy
   admin_categories GET    /admin/categories(.:format)                 admin/categories#index
                    POST   /admin/categories(.:format)                 admin/categories#create
 new_admin_category GET    /admin/categories/new(.:format)             admin/categories#new
edit_admin_category GET    /admin/categories/:id/edit(.:format)        admin/categories#edit
     admin_category GET    /admin/categories/:id(.:format)             admin/categories#show
                    PUT    /admin/categories/:id(.:format)             admin/categories#update
                    DELETE /admin/categories/:id(.:format)             admin/categories#destroy
              admin GET    /admin(.:format)                            admin/posts#index
          completed GET    /payment_completed(.:format)                payment#completed
            android GET    /android(.:format)                          hockeyapps#android
              login POST   /login(.:format)                            registrations#login
      loginfacebook POST   /loginfacebook(.:format)                    registrations#loginfacebook
             status GET    /status(.:format)                           registrations#status
             logout DELETE /logout(.:format)                           registrations#logout
             geturl POST   /geturl/:id/:token(.:format)                paypals#geturl
          getstatus POST   /getstatus/:id/:token(.:format)             paypals#getstatus
  payment_completed GET    /payment_completed(.:format)                payment#completed
    forgot_password GET    /forgot_password/:id(.:format)              passwords#forgot_password
      send_password POST   /send_password(.:format)                    passwords#send_password

这里有完整的文件routes.rb:

  scope "(:locale)", :constraints => {:locale => /[a-zA-Z]{2}/} do

    match '/faq' => 'site#faq', :via => :get

    match '/rules' => 'site#rules', :via => :get

    match '/advanced_rules' => 'site#advanced_rules', :via => :get

    match '/play_now' => 'play#play', :via => :get
    match '/play' => 'site#play', :via => :get

    match '/home' => 'site#home', :via => :get
    match '/qrcode/(:code)' => 'site#qrcode', :via => :get

    match '/forgot_password/:id' => 'passwords#forgot_password', :via => :get, :as => "forgot_password"
    match '/android_redirect' => 'android#index', :via => :get, :as => "android_redirect"

    match '/general_conditions' => "site#general_conditions", :via => :get

    match '/support' => "site#support", :via => :get

    match '/invitation/(:source)' => 'invitation#redirection', :via => :get

    match '/release_notes' => 'site#release_notes', :via => :get, :as => "release_notes"
    match '/release_notes/:version' => 'release_notes#show', :via => :get, :as => "release_note"

    root :to => 'site#home', :via => :get
  end

  namespace :admin do
    resources :posts , :categories
    match '/' => 'posts#index', :via => :get
  end

  get 'payment_completed' => 'payment#completed', :as => "completed"

  get 'android' => 'hockeyapps#android', :as => 'android'


  post 'login' => 'registrations#login', :as => 'login'
  post 'loginfacebook' => 'registrations#loginfacebook', :as => 'loginfacebook'
  get 'status' => 'registrations#status', :as => 'status'
  delete 'logout' => 'registrations#logout', :as => 'logout'

  post 'geturl/:id/:token' => 'paypals#geturl', :as => 'geturl'
  post 'getstatus/:id/:token' => 'paypals#getstatus', :as => 'getstatus'

  get 'payment_completed' => 'payment#completed', :as => 'payment_completed'

  #scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
  #  resources :passwords
  #end
  get 'forgot_password/:id' => 'passwords#forgot_password', :as => 'forgot_password'
  post 'send_password' => 'passwords#send_password', :as => 'send_password'
end

关于这个问题的任何想法?

感谢

2 个答案:

答案 0 :(得分:1)

match '/'root :to => ...和命名空间处理。因此路由尝试在模块(命名空间)site#home中调用admin

在定义root :to

之前,尝试将命名空间部分分别放在root或匹配'/ admin'前面

答案 1 :(得分:0)

试试这个

namespace :admin do
 resources :posts , :categories
 get '', to: 'posts#index', as: '/'
end

scope "(:locale)", :constraints => {:locale => /[a-zA-Z]{2}/} do
 ...
  root :to => 'site#home', :via => :get
 ...
end

rake routes

的输出
 admin GET    /admin(.:format)                 admin/posts#index

尝试获取localhost:3000/admin

Started GET "/admin" for 127.0.0.1 at 2013-06-11 17:41:40 +0700
Processing by Admin::PostsController#index as HTML
  Rendered admin/posts/index.html.erb within layouts/application (1.0ms)
Completed 200 OK in 1095ms (Views: 1095.1ms | ActiveRecord: 0.0ms)