【发布时间】:2016-06-23 06:11:15
【问题描述】:
考虑我在控制器中有这样的 show 方法:
class ThingsController < ApplicationController
def show
thing = Thing.find_by_id(params[:id])
render json: 'Not Found', status: :not_found and return if !thing
render json: thing.to_json, status: :ok
end
end
数据库中只有 1 条 id=1 的记录。
现在,这是我的测试:
- 当我调用 /things/1 时,我可以获得记录。
- 当我调用 /things/2 时,我得到 404。
- 当我调用 /things/a1 时,我得到 404。
- 当我调用 /things/1a 时,我预计会出错,但它给了我 id=1 的记录。
#4 正常吗?如何预防?
Rails 版本:4.2.6
【问题讨论】:
标签: ruby-on-rails activerecord