我试图学习稍微干掉我的代码,但遇到了一个问题,
我使用控制器和路径选项确定我的路线
scope path: '/administrators', controller: :administrators do
get 'unverified' => :unverified
patch 'verify/:id' => :verify
get 'reported' => :reported
get 'ban_user' => :ban_user
patch 'execute_ban/:id' => :execute_ban
end
所以这是我到目前为止所做的,所有get
链接都正常工作......
但是这会使patch 'execute_ban/:id' => :execute_ban
成为ban user
的扩展名:(也与验证相同)
verified GET /administrators/unverified(.:format) administrators#unverifie
PATCH /administrators/verify/:id(.:format) administrators#verify
reported GET /administrators/reported(.:format) administrators#reported
ban_user GET /administrators/ban_user(.:format) administrators#ban_user
PATCH /administrators/execute_ban/:id(.:format) administrators#execute_ban
现在我改变了我的link_to route = link_to 'ban', ban_user_path(x.id), method: :patch
但它抛出路由错误,说没有路径匹配。
是否有一些东西丢失了,任何洞察力都会一如既往地被证实。
由于
答案 0 :(得分:0)
为什么不采用围绕被修改资源的更加宁静的设计呢?
Rails.application.routes.draw do
resources :users do
get :unverified, on: :collection
get :reported, on: :collection
get :ban
patch :ban, action: 'execute_ban'
end
end
您不需要表单的不同路径并作用于资源 - 请改用HTTP动词。
Prefix Verb URI Pattern Controller#Action
unverified_users GET /users/unverified(.:format) users#unverified
reported_users GET /users/reported(.:format) users#reported
user_ban GET /users/:user_id/ban(.:format) users#ban
PATCH /users/:user_id/ban(.:format) users#execute_ban
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
如果您只是为了授权目的而添加一个单独的控制器,那么这是一种反模式,因为您只是为Pundit / CanCanCan应该处理的事情增加了更多的复杂性。