【发布时间】:2021-10-26 10:06:36
【问题描述】:
当我访问时:
网址/打捞/25605
ActiveRecord 显示错误 RecordNotFound
没有 ID 无法找到 Salvage
参数:
{"id"=>"25605"}
当我在参数中包含“typeID”时,为什么 ActiveRecord 会搜索“id”?
控制器
class SalvageController < ApplicationController
def show
@salvage = Salvage.find(params[:typeID])
end
型号
class Salvage < ApplicationRecord
validates :typeID, presence: true
validates :amount, presence: true
end
查看
<%= @salvage.typeID %>, <%= @salvage.amount %>
资源
resources :salvage
铁路路线
Prefix Verb URI Pattern Controller#Action
salvage_new GET /salvage/new(.:format) salvage#new
root GET / application#hello
production_pages_copying GET /production_pages/copying(.:format) production_pages#copying
salvage_index GET /salvage(.:format) salvage#index
POST /salvage(.:format) salvage#create
new_salvage GET /salvage/new(.:format) salvage#new
edit_salvage GET /salvage/:id/edit(.:format) salvage#edit
salvage GET /salvage/:id(.:format) salvage#show
PATCH /salvage/:id(.:format) salvage#update
PUT /salvage/:id(.:format) salvage#update
DELETE /salvage/:id(.:format) salvage#destroy
我第二次使用我自己的数据在 learnenough.com 上关注 ruby on rails 第 6 版教程
【问题讨论】:
-
您能否添加
rake routes | grep salvage的结果并从rails 服务器日志中请求参数 -
啊哈! '打捞 GET /salvage/:id(.:format)' @DeepakMahakale 有趣...
-
顺便说一下,在 rails 6.1 中,rake 路线被贬值,而有利于 rails 路线github.com/rails/rails/commit/…
-
@DeepakMahakale 我在原始问题中包含了 rails 路线的结果