路线/专辑/新地图到专辑#show

时间:2017-08-10 20:24:27

标签: ruby-on-rails routing

rails edge文档说:

  

GET | / albums / new |专辑#new |返回一个用于创建的HTML表单   新专辑

但是当我尝试这样做时,我得到了:

Started GET "/api/albums/new" for 172.18.0.1 at 2017-08-10 20:12:06 +0000
Processing by API::AlbumsController#show as */*

这是关于专辑的路由器:

Rails.application.routes.draw do
  namespace :api, constraints: {format: :json} do
    resources :albums
    put     '/albums/:id/photo/:photo'  => "albums#add_photo"
    put     '/albums/:id/bucket'        => "albums#add_bucket"
    get     '/albums/:id/photos'        => 'albums#photos'
  end
end

问题: 那么为什么/ api / albums / new映射到API :: AlbumsController#show而不是API :: AlbumsController #new

增加:

我的rake routes输出:

                     Prefix Verb   URI Pattern                                  Controller#Action
            api_users_login POST   /api/users/login(.:format)                   api/users#authenticate {:format=>:json}
       api_users_from_token GET    /api/users/from_token(.:format)              api/users#validate_token {:format=>:json}
                  api_users GET    /api/users(.:format)                         api/users#index {:format=>:json}
                            POST   /api/users(.:format)                         api/users#create {:format=>:json}
                   api_user GET    /api/users/:id(.:format)                     api/users#show {:format=>:json}
                            PATCH  /api/users/:id(.:format)                     api/users#update {:format=>:json}
                            PUT    /api/users/:id(.:format)                     api/users#update {:format=>:json}
                            DELETE /api/users/:id(.:format)                     api/users#destroy {:format=>:json}
                 api_albums GET    /api/albums(.:format)                        api/albums#index {:format=>:json}
                            POST   /api/albums(.:format)                        api/albums#create {:format=>:json}
                  api_album GET    /api/albums/:id(.:format)                    api/albums#show {:format=>:json}
                            PATCH  /api/albums/:id(.:format)                    api/albums#update {:format=>:json}
                            PUT    /api/albums/:id(.:format)                    api/albums#update {:format=>:json}
                            DELETE /api/albums/:id(.:format)                    api/albums#destroy {:format=>:json}
                        api PUT    /api/albums/:id/photo/:photo(.:format)       api/albums#add_photo {:format=>:json}
                            PUT    /api/albums/:id/bucket(.:format)             api/albums#add_bucket {:format=>:json}
                            GET    /api/albums/:id/photos(.:format)             api/albums#photos {:format=>:json}
                  api_pages GET    /api/pages(.:format)                         api/pages#index {:format=>:json}
         api_photos_taglist GET    /api/photos/taglist(.:format)                api/photos#taglist {:format=>:json}
          api_photos_bucket GET    /api/photos/bucket(.:format)                 api/photos#bucket {:format=>:json}
                            POST   /api/photos/bucket/rotate/:degrees(.:format) api/photos#rotate_bucket {:format=>:json}
                 api_photos GET    /api/photos(.:format)                        api/photos#index {:format=>:json}
                            POST   /api/photos(.:format)                        api/photos#create {:format=>:json}
                  api_photo GET    /api/photos/:id(.:format)                    api/photos#show {:format=>:json}
                            PATCH  /api/photos/:id(.:format)                    api/photos#update {:format=>:json}
                            PUT    /api/photos/:id(.:format)                    api/photos#update {:format=>:json}
                            DELETE /api/photos/:id(.:format)                    api/photos#destroy {:format=>:json}
                            GET    /api/photos/:id/rotate(/:degrees)(.:format)  api/photos#rotate {:format=>:json}
                            POST   /api/photos/:id/comment/add(.:format)        api/photos#comment {:format=>:json}
                            DELETE /api/photos/:id/comment/delete(.:format)     api/photos#uncomment {:format=>:json}
                            POST   /api/photos/:id/tag/add(.:format)            api/photos#tag {:format=>:json}
                            DELETE /api/photos/:id/tag/delete(.:format)         api/photos#untag {:format=>:json}
                            POST   /api/photos/:id/like/toggle(.:format)        api/photos#like_toggle {:format=>:json}
                            POST   /api/photos/:id/bucket/toggle(.:format)      api/photos#bucket_toggle {:format=>:json}
                 api_facets GET    /api/facets(.:format)                        api/facets#index {:format=>:json}
                            POST   /api/facets(.:format)                        api/facets#create {:format=>:json}
                  api_facet GET    /api/facets/:id(.:format)                    api/facets#show {:format=>:json}
                            PATCH  /api/facets/:id(.:format)                    api/facets#update {:format=>:json}
                            PUT    /api/facets/:id(.:format)                    api/facets#update {:format=>:json}
                            DELETE /api/facets/:id(.:format)                    api/facets#destroy {:format=>:json}
api_catalogs_oauth_callback GET    /api/catalogs/oauth_callback(.:format)       api/catalogs#oauth_callback {:format=>:json}
                            GET    /api/catalogs/:id/import(.:format)           api/catalogs#import {:format=>:json}
                            GET    /api/catalogs/:id/photos(.:format)           api/catalogs#photos {:format=>:json}
               api_catalogs GET    /api/catalogs(.:format)                      api/catalogs#index {:format=>:json}
                            POST   /api/catalogs(.:format)                      api/catalogs#create {:format=>:json}
                api_catalog GET    /api/catalogs/:id(.:format)                  api/catalogs#show {:format=>:json}
                            PATCH  /api/catalogs/:id(.:format)                  api/catalogs#update {:format=>:json}
                            PUT    /api/catalogs/:id(.:format)                  api/catalogs#update {:format=>:json}
                            DELETE /api/catalogs/:id(.:format)                  api/catalogs#destroy {:format=>:json}
    api_locations_countries GET    /api/locations/countries(.:format)           api/locations#countries {:format=>:json}
       api_locations_cities GET    /api/locations/cities(.:format)              api/locations#cities {:format=>:json}
              api_locations GET    /api/locations(.:format)                     api/locations#index {:format=>:json}
                            POST   /api/locations(.:format)                     api/locations#create {:format=>:json}
               api_location GET    /api/locations/:id(.:format)                 api/locations#show {:format=>:json}
                            PATCH  /api/locations/:id(.:format)                 api/locations#update {:format=>:json}
                            PUT    /api/locations/:id(.:format)                 api/locations#update {:format=>:json}
                            DELETE /api/locations/:id(.:format)                 api/locations#destroy {:format=>:json}
             api_photofiles GET    /api/photofiles(.:format)                    api/photofiles#index {:format=>:json}
                            POST   /api/photofiles(.:format)                    api/photofiles#create {:format=>:json}
              api_photofile GET    /api/photofiles/:id(.:format)                api/photofiles#show {:format=>:json}
                            PATCH  /api/photofiles/:id(.:format)                api/photofiles#update {:format=>:json}
                            PUT    /api/photofiles/:id(.:format)                api/photofiles#update {:format=>:json}
                            DELETE /api/photofiles/:id(.:format)                api/photofiles#destroy {:format=>:json}
                            GET    /api/photofiles/:id/photoserve(.:format)     api/photofiles#photoserve {:format=>:json}
                            PATCH  /api/photofiles/:id/rotate(.:format)         api/photofiles#rotate {:format=>:json}
                            GET    /api/photofiles/:id/phash(.:format)          api/photofiles#phash {:format=>:json}
              api_jobs_list POST   /api/jobs/list(.:format)                     api/jobs#list {:format=>:json}
                   api_jobs GET    /api/jobs(.:format)                          api/jobs#index {:format=>:json}
                            POST   /api/jobs(.:format)                          api/jobs#create {:format=>:json}
                    api_job GET    /api/jobs/:id(.:format)                      api/jobs#show {:format=>:json}
                            PATCH  /api/jobs/:id(.:format)                      api/jobs#update {:format=>:json}
                            PUT    /api/jobs/:id(.:format)                      api/jobs#update {:format=>:json}
                            DELETE /api/jobs/:id(.:format)                      api/jobs#destroy {:format=>:json}
                                   /resque                                      #<Resque::Server app_file="/usr/local/bundle/gems/resque-1.27.4/lib/resque/server.rb">
                       root GET    /                                            photos#index

1 个答案:

答案 0 :(得分:1)

根据您的路线,以及(正如您在评论中指出的那样)您的--apionly应用是一个事实,没有new动作,也没有edit动作。它们(通常)在API中不是有用的概念;正如文档所述,它通常用于呈现HTML表单,而这在HTML中没有任何地方。

GET "/api/albums/new"路径路由到API::AlbumsController#show,因为它与GET "/api/albums/:id"匹配,因此"new"被解释为标识符。