Rails集合路由连接到:show action

时间:2016-05-13 09:23:56

标签: ruby-on-rails

这是我的route.rb文件:

namespace :api, defaults: { format: 'json' } do
  namespace :v1 do
    resources :projects, :only => [:index, :create, :update, :destroy, :show] do
      resources :events, shallow: true
      resources :comments, shallow: true
      resources :posts, shallow: true
      resources :users, shallow: true
      resources :project_participations, shallow: true
    end

    resources :users, :only => [:show, :index] do
      collection do
        get :search
        post :register_push_receiver_device_id
        delete :deregister_push_receiver_device_id
      end
    end
  end
end

当我向/ api / v1 / users / search发出请求时,这就是服务器的响应方式。

Started GET "/api/v1/users/search?search_string=jason" for 127.0.0.1 at 2016-05-13 18:11:02 +0900
Processing by API::V1::UsersController#show as JSON
  Parameters: {"search_string"=>"jason", "id"=>"search", "user"=>{}}
  User Load (3.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'EMAIL'  ORDER BY "users"."id" ASC LIMIT 1
  User Load (4.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'EMAIL' AND "users"."authentication_token" = 'TOKEN'  ORDER BY "users"."id" ASC LIMIT 1
  User Load (2.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 0 LIMIT 1
Filter chain halted as :set_user rendered or redirected
Completed 404 Not Found in 70ms (Views: 0.4ms | ActiveRecord: 17.9ms)

如您所见,请求被路由到:以“搜索”作为ID显示操作,从而导致错误。

有趣的是,当我使用route.rb文件中的用户资源切换项目资源的顺序时,请求功能正常。

namespace :api, defaults: { format: 'json' } do
  namespace :v1 do
    resources :users, :only => [:show, :index] do
      collection do
        get :search
        post :register_push_receiver_device_id
        delete :deregister_push_receiver_device_id
      end
    end

    resources :projects, :only => [:index, :create, :update, :destroy, :show] do
      resources :events, shallow: true
      resources :comments, shallow: true
      resources :posts, shallow: true
      resources :users, shallow: true
      resources :project_participations, shallow: true
    end
  end
end

在这种情况下,请求处理正确。

Started GET "/api/v1/users/search?search_string=jason" for 127.0.0.1 at 2016-05-13 18:21:14 +0900
Processing by API::V1::UsersController#search as JSON
  Parameters: {"search_string"=>"jason", "user"=>{}}
  User Load (2.9ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'EMAIL'  ORDER BY "users"."id" ASC LIMIT 1
  User Load (2.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'EMAIL' AND "users"."authentication_token" = 'TOKEN'  ORDER BY "users"."id" ASC LIMIT 1
  User Load (80.6ms)  SELECT "users".* FROM "users"  WHERE "users"."status" = 'member' AND (name LIKE '%jason%')  ORDER BY "users"."name" ASC
  Rendered api/v1/users/search.json.jbuilder (86.4ms)
Completed 200 OK in 248ms (Views: 90.0ms | ActiveRecord: 99.7ms)

我已阅读过Rails路由文档,但我无法理解这种行为。有人可以解释为什么会发生这种情况吗?

2 个答案:

答案 0 :(得分:1)

这是因为shallowprojects/users路由,它定义了/api/v1/users/:id路由。

查看您的rake routes

答案 1 :(得分:1)

  

:用户范围:项目不应该影响:用户本身!

不,当您运行rake routes时,您会看到以下路线。

user GET  /users/:id(.:format)  users#show

这是因为shallow :trueresources :users。这会为您提供一些 非嵌套路由 ,其中也包含show路由(是目前困扰您的问题 >)。所以 订单 在这里很重要。