Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 有没有更好的方法来定义这些路线?_Ruby On Rails_Ruby_Url Routing - Fatal编程技术网

Ruby on rails 有没有更好的方法来定义这些路线?

Ruby on rails 有没有更好的方法来定义这些路线?,ruby-on-rails,ruby,url-routing,Ruby On Rails,Ruby,Url Routing,我正在Rails应用程序中为用户提供支持。用户甚至不需要知道还有其他用户。我不想只使用资源:用户,因为生成的路由是: 用户获取/users用户#索引 发布/用户用户#创建 新用户获取/用户/新用户#新 编辑用户获取/用户/:id/编辑用户#编辑 用户获取/users/:id用户#显示 补丁/用户/:id用户#更新 PUT/users/:id用户#更新 删除/users/:id用户#销毁 (:format)已删除,以提高可读性 您必须在URL中输入用户id号,这样用户就有机会知道存在其他用户。我

我正在Rails应用程序中为用户提供支持。用户甚至不需要知道还有其他用户。我不想只使用
资源:用户
,因为生成的路由是:

用户获取/users用户#索引
发布/用户用户#创建
新用户获取/用户/新用户#新
编辑用户获取/用户/:id/编辑用户#编辑
用户获取/users/:id用户#显示
补丁/用户/:id用户#更新
PUT/users/:id用户#更新
删除/users/:id用户#销毁
(:format)
已删除,以提高可读性

您必须在URL中输入用户id号,这样用户就有机会知道存在其他用户。我想要这些路线:

用户获取/users用户#索引
发布/用户用户#创建
新用户获取/用户/新用户#新
编辑用户获取/用户/我/编辑用户#编辑
用户获取/用户/me用户#显示
修补程序/用户/me用户#更新
PUT/users/me users#更新
删除/用户/me用户#销毁
/users/me
是您的用户路径,这是您可以获取的唯一用户路径

但问题是如何定义这些路线。这里有一个想法:

resources :users, constraints: { id: 'me' }
用户
模型中:

def to_param
  'me'
end
但这对我来说太麻烦了。有更好的主意吗?

和你的同事一起去吧;-)

按如下方式定义您的路线:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
如果您离开
路径:
选项,您的路线将是单条的(
/user
)。玩转选项;-)

您的
rake routes
结果应该如下所示:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
您还可以在Rails控制台上测试路由(
Rails c


如果需要,可以在上对
:user
进行复数,但不要忘记将
设置为:
选项,例如:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :users, path: '/users/me', as: 'user', only: [:show, :edit, :update, :destroy]

请查看Rails指南中的注意事项和警告!以下是摘录:

一个长期存在的错误阻止form_for自动处理单一资源

有了你的支持,你就可以走了;-)

按如下方式定义您的路线:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
如果您离开
路径:
选项,您的路线将是单条的(
/user
)。玩转选项;-)

您的
rake routes
结果应该如下所示:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
您还可以在Rails控制台上测试路由(
Rails c


如果需要,可以在上对
:user
进行复数,但不要忘记将
设置为:
选项,例如:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :users, path: '/users/me', as: 'user', only: [:show, :edit, :update, :destroy]

请查看Rails指南中的注意事项和警告!以下是摘录:

一个长期存在的错误阻止form_for自动处理单一资源

有了你的支持,你就可以走了;-)

按如下方式定义您的路线:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
如果您离开
路径:
选项,您的路线将是单条的(
/user
)。玩转选项;-)

您的
rake routes
结果应该如下所示:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
您还可以在Rails控制台上测试路由(
Rails c


如果需要,可以在上对
:user
进行复数,但不要忘记将
设置为:
选项,例如:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :users, path: '/users/me', as: 'user', only: [:show, :edit, :update, :destroy]

请查看Rails指南中的注意事项和警告!以下是摘录:

一个长期存在的错误阻止form_for自动处理单一资源

有了你的支持,你就可以走了;-)

按如下方式定义您的路线:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
如果您离开
路径:
选项,您的路线将是单条的(
/user
)。玩转选项;-)

您的
rake routes
结果应该如下所示:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
   Prefix Verb   URI Pattern              Controller#Action
    users GET    /users(.:format)         users#index
          POST   /users(.:format)         users#create
 new_user GET    /users/new(.:format)     users#new
edit_user GET    /users/me/edit(.:format) users#edit
     user GET    /users/me(.:format)      users#show
          PATCH  /users/me(.:format)      users#update
          PUT    /users/me(.:format)      users#update
          DELETE /users/me(.:format)      users#destroy
您还可以在Rails控制台上测试路由(
Rails c


如果需要,可以在上对
:user
进行复数,但不要忘记将
设置为:
选项,例如:

# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :users, path: '/users/me', as: 'user', only: [:show, :edit, :update, :destroy]

请查看Rails指南中的注意事项和警告!以下是摘录:

一个长期存在的错误阻止form_for自动处理单一资源