首页 > 解决方案 > 导致遗留问题的路线轨道 5,不能破坏等

问题描述

这是我之前问题的延续:Put or patch for new update action Rails

这个问题的解决方案有效,但是,该解决方案在遗留系统中引起了其他问题,我将展示主要的两个问题,但我不确定如何让系统和平工作,直到我可以着手重构系统。

使用post :update以下操作中断,销毁任何东西,导入记录,如果我取消注释post :update然后导入作品,编辑不会。

没有一个表单设置为使用 REST,我目前无法更改,因为我现在正在尝试处理路由,然后移动到系统本身。

这是路线的示例

resources :stock_groups, except: %i[destroy] do
   member do
    get :copy

    post :copy
    post :update # temp PATCH, PUT routes
  end

 collection do
   get :list
   get :import_stock_groups
   get :download_stock_groups_template

   post :preview_import_stock_groups
   post :process_import_stock_groups
 end
end

# remap wrong implmentation of paths
get '/stock_groups/edit/:id', to: redirect('/stock_groups/%{id}/edit')
get '/stock_groups/copy/:id', to: redirect('/stock_groups/%{id}/copy')
get '/stock_groups/show/:id', to: redirect('/stock_groups/%{id}')

这是我在导入记录时得到 在此处输入图像描述 的结果,它针对控制器中的错误方法。

至于破坏它也失败了,我确实尝试添加类似的东西,post :destroy但这没有用。

任何帮助都会很棒。

标签: ruby-on-railsmodel-view-controllerroutesruby-on-rails-5

解决方案


好吧,让我们讨论一下错误,它告诉您这里发生了什么。您正在尝试搜索 StockGroup.find(params[:id]) 应该是整数。因为 find 将按 id 值搜索您的记录。

StockGroup.find(params[:id]) # only integer allowed

但是在您发送的请求中id = process_import_stock_groups,这是不可能作为 rails 约定的,除非您通过更改 rails 约定在模型级别更改它,否则您将拥有 id 整数。所以要试运行rails c

StockGroup.find('process_import_stock_groups') # should not work but just try to know your error

现在您将此值保存在哪一列中,process_import_stock_groups因此您的方法应该是

StockGroup.where(name_of_column: 'process_import_stock_groups').first

你得到什么?我希望它能解决你的问题。


推荐阅读