【问题标题】:Rails disable edit update delete routesRails 禁用编辑更新删除路线
【发布时间】:2019-02-15 20:05:09
【问题描述】:

我正在尝试找到一种方法来禁用资源路由,例如编辑销毁和更新。可以使用这个答案来完成。 Disable Route 在这个答案中我可以这样写代码:

resources :books, except: [:edit, :destroy]

它会起作用,但我有一个独特的问题,我创建了许多资源路由,我的路由文件是这样的:

         resources :expenditure_management2s do 
                            collection { post :import }
                            collection { get :dropdown }
                            collection { get :test }
                            end 
        resources :expenditure_management1s do 
                            collection { post :import }
                            collection { get :dropdown }
                            collection { get :test }
                            end 
        resources :expenditure_managements do 
                            collection { post :import }
                            collection { get :dropdown }
                            collection { get :test }
                            end 
                 ......

我有近 100 条这样的路线如果我必须逐个更改这些方法,那将是一项艰巨的任务。有什么方法可以将这些路由分组到某个方法中并拒绝所有资源路由的编辑更新和销毁。

【问题讨论】:

  • 你用的是什么编辑器?
  • 可视化代码编辑器

标签: ruby-on-rails routes


【解决方案1】:

我认为您可以像这样在 routes.rb 文件中使用范围:

scope except: [:edit, :destroy] do
  resources :users
end

将返回路线:

users     GET   /users(.:format)   users#index
          POST  /users(.:format)   users#create
new_user  GET   /users/new(.:format)   users#new
user      GET   /users/:id(.:format)   users#show
          PATCH /users/:id(.:format)   users#update
          PUT   /users/:id(.:format)   users#update

如您所见,users#destroyusers#edit 路由丢失了。

你的情况是:

 scope except: [:edit, :destroy] do
    resources :expenditure_management2s do 
                        collection { post :import }
                        collection { get :dropdown }
                        collection { get :test }
                        end 
    resources :expenditure_management1s do 
                        collection { post :import }
                        collection { get :dropdown }
                        collection { get :test }
                        end 
    resources :expenditure_managements do 
                        collection { post :import }
                        collection { get :dropdown }
                        collection { get :test }
                        end 
 end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2018-07-26
    相关资源
    最近更新 更多