使用rails-api
:
def show
render json: Room.find(params[:id])
end
这在找到资源时有效。但是寻找一个不存在的会返回 500 错误。这不应该返回 404 吗?
> http --json GET chat.dev/api/v1/rooms/23
HTTP/1.1 500 Internal Server Error
Connection: close
Content-Type: text/plain; charset=utf-8
X-Request-Id: 4cd2ba9f-0f85-4530-9c0a-0ef427ac5b31
X-Runtime: 0.094633
ActiveRecord::RecordNotFound at /api/v1/rooms/23
================================================
> Couldn't find Room with id=23
app/controllers/api/v1/rooms_controller.rb, line 20
---------------------------------------------------
``` ruby
15 # render :json => {}, :status => :not_found
16 # end
17 # end
18
19 def show
> 20 render json: Room.find(params[:id])
21 end
22
23 end
24
25 end