Ruby-on-rails Rails 路由:不带参数的 GET :id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17066637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:09:22  来源:igfitidea点击:

Rails routes: GET without param :id

ruby-on-railsrubyruby-on-rails-3.2routes

提问by Gozup

I'm developing a REST api based on rails. To use this api, you MUST be logged in. Regarding that, I'd like to create a method mein my user controller that will return a json of the logged in user infos. So, I don't need an :idto be passed in the URL. I just want to call http://domain.com/api/users/me

我正在开发一个基于 Rails 的 REST api。要使用这个 api,你必须登录。关于这一点,我想me在我的用户控制器中创建一个方法,它将返回登录用户信息的 json。所以,我不需要:id在 URL 中传递一个。我只想调用http://domain.com/api/users/me

So I tried this:

所以我试过这个:

namespace :api, defaults: { format: 'json' } do
  scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
    resources :tokens, :only => [:create, :destroy]
    resources :users, :only => [:index, :update] do

      # I tried this
      match 'me', :via => :get
      # => api_user_me GET    /api/users/:user_id/me(.:format)       api/v1/users#me {:format=>"json"}

      # Then I tried this
      member do
        get 'me'
      end
      # => me_api_user GET    /api/users/:id/me(.:format)            api/v1/users#me {:format=>"json"}

    end
  end
end

As you can see, my route waits for an id, but I'd like to get something like devise has. Something based on current_userid. Example below:

如您所见,我的路线等待一个 id,但我想得到类似 devise 的东西。基于current_userid 的东西。下面的例子:

edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit

In this example you can edit the current user password without passing the id as a param.

在此示例中,您可以编辑当前用户密码,而无需将 id 作为参数传递。

I could use a collection instead of a member, but that's a dirty bypass...

我可以使用集合而不是成员,但这是一个肮脏的绕过......

Anyone has an idea? Thank you

有人有想法吗?谢谢

采纳答案by Arjan

Resource routes are designed to work this way. If you want something different, design it yourself, like this.

资源路由旨在以这种方式工作。如果您想要不同的东西,请自己设计,就像这样。

match 'users/me' => 'users#me', :via => :get

Put it outside of your resources :usersblock

把它放在你的resources :users街区之外

回答by Waiting for Dev...

The way to go is to use singular resources:

要走的路是使用单一资源

So, instead of resourcesuse resource:

所以,而不是resources使用resource

Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action [...]

有时,您有一个资源,客户端总是在不引用 ID 的情况下查找该资源。例如,您希望 /profile 始终显示当前登录用户的个人资料。在这种情况下,您可以使用单一资源将 /profile(而不是 /profile/:id)映射到显示操作 [...]

So, in your case:

所以,在你的情况下:

resource :user do
  get :me, on: :member
end

# => me_api_user GET    /api/users/me(.:format)            api/v1/users#me {:format=>"json"}

回答by EfratBlaier

Maybe I am missing something, but why don't you use:

也许我错过了一些东西,但你为什么不使用:

get 'me', on: :collection

回答by leandrotk

You can use

您可以使用

resources :users, only: [:index, :update] do
  get :me, on: :collection
end

or

或者

resources :users, only: [:index, :update] do
  collection do
    get :me
  end
end

"A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object. Search is an example of a collection route, because it acts on (and displays) a collection of objects." (from here)

“成员路由需要一个 ID,因为它作用于成员。集合路由不需要,因为它作用于对象集合。预览是成员路由的一个例子,因为它作用于(并显示)单个对象。搜索是收集路线的一个例子,因为它作用于(并显示)一组对象。” (从这里

回答by manohar

  resources :users, only: [:index, :update] do
    collection do
      get :me, action: 'show' 
    end
  end

specifying the action is optional. you can skip action here and name your controller action as me.

指定操作是可选的。您可以在此处跳过操作并将您的控制器操作命名为me.

回答by Raf

This gives same result as Arjan's in simpler way

这以更简单的方式给出与 Arjan 相同的结果

get 'users/me', to: 'users#me'

get 'users/me', to: 'users#me'

回答by manoj

When you create a route nested within a resource, you can mention, whether it is member action or a collection action.

当你创建一个嵌套在资源中的路由时,你可以提到它是成员动作还是集合动作。

namespace :api, defaults: { format: 'json' } do
  scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
    resources :tokens, :only => [:create, :destroy]
    resources :users, :only => [:index, :update] do

      # I tried this
      match 'me', :via => :get, :collection => true
...
...