ruby-on-rails - 导致遗留问题的路线轨道 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
但这没有用。
任何帮助都会很棒。
解决方案
好吧,让我们讨论一下错误,它告诉您这里发生了什么。您正在尝试搜索 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
你得到什么?我希望它能解决你的问题。
推荐阅读
- java - 删除 AlertDialog 中项目顶部和底部的空格
- javascript - 编写异步函数的具体困惑。任何人都可以验证吗?
- java - Groovy 使用 RegEx 拆分获取其他 2 个字符串之间的字符串
- javascript - 无法将 php 密码字段上传到 phpmyAdmin
- vba - For循环与另一个范围内的单元格范围
- android - 如何在 Android 中访问 google fit 用户的电子邮件
- react-native - 如何禁用反应导航的堆栈导航器转换?
- chef-infra - 如何在厨师食谱中运行一批 sql 文件
- ruby-on-rails - rails嵌套资源没有路由匹配缺少必需的键[:id]
- android - Firebase 操作似乎正在自行清除我的 EditText