看到许多与此相关的问题,但没有一个答案给我的问题。 我有没有ActiveRecord支持的Rails API应用程序。很容易重现问题。按照步骤: 没有ActiveRecord的支持创建轨API网站没有路线匹配{:action =>“show”...缺少必需的键:[:id]
rails new test001 -O --api
更改文件夹test001并运行:
rails g scaffold testapp id name
创建在app/models文件夹的模型文件testapp.rb
class Testapp
include ActiveModel::Model
attr_accessor :id, :name, :created_at, :updated_at
def self.all
t1 = Testapp.new(:id =>111, name: "t111")
return [t1]
end
end
启动服务器
rails s
使用邮递员REST客户端创建GET请求
http://localhost:3000/testapps.json
它失败,错误ActionView::Template::Error (No route matches {:action=>"show", :controller=>"testapps", :format=>:json, :id=>#<Testapp:0x00000005411518 @id=111, @name="t111">} missing required keys: [:id]):
我有POST虚拟实现,PUT,GET 1项,所有的作品。这里是虚拟实现GET 1项(/testapps/x.json)
def self.find(p)
return Testapp.new(:id =>p, name: "t123")
end
什么是让所有(/testapps.json)的问题?
为了验证可能的答案,如果您可以发布您的'routes.rb'文件或该文件的特定部分,其中您为'testapps'设置了路由控制器。 –
'Rails.application.routes.draw do resources:testapps end' 正如我所说的,所有其他的作品。 –
@ craig.kaminsky 您可以从[dropbox]下载应用程序(https://dl.dropboxusercontent.com/u/10850412/test001.zip) –