2016-04-29 103 views
0

看到许多与此相关的问题,但没有一个答案给我的问题。 我有没有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)的问题?

+0

为了验证可能的答案,如果您可以发布您的'routes.rb'文件或该文件的特定部分,其中您为'testapps'设置了路由控制器。 –

+0

'Rails.application.routes.draw do resources:testapps end' 正如我所说的,所有其他的作品。 –

+0

@ craig.kaminsky 您可以从[dropbox]下载应用程序(https://dl.dropboxusercontent.com/u/10850412/test001.zip) –

回答

1

找到解决办法。 问题是支架产生index.json.jbuilder 文件:

json.array!(@testapps) do |testapp| 
    json.extract! testapp, :id, :id, :name 
    json.url testapp_url(testapp, format: :json) #REMOVE 
end 

它补充线json.url testapp_url(testapp,格式::JSON)无故。 json.extract!已反序列化的对象。 删除行解决的问题。 我仍然不知道为什么testapp_url(testapp,format:json)导致错误。检查导轨路由文档http://guides.rubyonrails.org/routing.html

相关问题