gpt4 book ai didi

ruby-on-rails - 降低路线的复杂性

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:39 26 4
gpt4 key购买 nike

我正在尝试设置我的 routes.rb 以便/sessions/不需要在 url 中登录和退出站点。下面是我的示例,以显示我正在努力实现的目标。虽然“第二次尝试”确实做了我想做的事,但我想知道是否有更有效的方法来做到这一点。我是 Rails 的新手,我确信 routes.rb 有一些选项可以完成我在三大行中所做的事情。

第一次尝试

路线.rb

namespace :account do
resources :users
resources :sessions
end

$ 佣金路线

                  Prefix Verb   URI Pattern                          Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_sessions GET /account/sessions(.:format) account/sessions#index
POST /account/sessions(.:format) account/sessions#create
new_account_session GET /account/sessions/new(.:format) account/sessions#new
edit_account_session GET /account/sessions/:id/edit(.:format) account/sessions#edit
account_session GET /account/sessions/:id(.:format) account/sessions#show
PATCH /account/sessions/:id(.:format) account/sessions#update
PUT /account/sessions/:id(.:format) account/sessions#update
DELETE /account/sessions/:id(.:format) account/sessions#destroy

第二次尝试

路线.rb

  namespace :account do
resources :users
match '/login', :controller => 'sessions', :action => 'new', :via => [:get]
match '/login', :controller => 'sessions', :action => 'create', :via => [:post]
match '/logout', :controller => 'sessions', :action => 'destroy', :via => [:delete]
end

$ 佣金路线

           Prefix Verb   URI Pattern                       Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_login GET /account/login(.:format) account/sessions#new
POST /account/login(.:format) account/sessions#create
account_logout DELETE /account/logout(.:format) account/sessions#destroy

是否可以在无需手动指定匹配位置的情况下完成此操作?我只想删除/sessions/作为要求。

最佳答案

namespace :account do
resources :users #-> account/users
resources :sessions, path: "", path_names: { new: "login", create: "login", destroy: "logout" } #-> accounts/login, accounts/logout
end

关于ruby-on-rails - 降低路线的复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23669092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com