2012-08-28 22 views
0

我创建了一个练习rails应用程序,其中创建了名称空间并版本化,如在此railscast中演示的那样。一切工作正常,我可以看到在在Rails API应用程序中使用版本控制 - 无法为特定控制器操作呈现JSON

然后,我添加了Rabl的宝石浏览器的JSON输出,并试图使Rabl的意见,但我得到浏览器中呈现一个空的JSON数组

这里是什么我系统地进行了版本控制工作

1) changed the routes file to look like this 

App0828::Application.routes.draw do 

namespace :api, defaults: { format: 'json'} do 
    namespace :v1 do 
    resources :vendors 
    end 
end 

    #resources :vendors 
    #root to: 'vendors#index' 
end 

2) The created thise files 
    app/copntrollers/api/v1/vendors_controller.rb 

    Inside the vendors_controller.rb I added the following code 

module Api 
    module V1 
    class VendorsController < ApplicationController 

    class Vendor < ::Vendor 
    #subclass vendor class so thatyou can extend its behaviour just for this version 
    #add any functions specific to this version here 
    end 

    respond_to :json 

    def index 
    respond_with Vendor.all  
    end 

    def show 
    respond_with Vendor.find(params[:id]) 
    end 
    .......... 

3) Then I pointed my browser to this url "http://localhost:3000/api/v1/vendors" 
    And I can see the json output in my browser 

4) Then I added the rabl gem 
5) restarted the server 
6) Changed the above file at app/copntrollers/api/v1/vendors_controller.rb 
    from the version above to the version below 

    module Api 
    module V1 
    class VendorsController < ApplicationController 

    class Vendor < ::Vendor 
     #subclass vendor class so thatyou can extend its behaviour just for this version 
     #add any functions specific to this version here 
    end 

    respond_to :json 

    def index 
    render 'api/v1/index.json.rabl' 
    end 

    def show 
    render 'api/v1/show.json.rabl' 
    end 

    ....... 
7) I created the following files with this code: 
    file: app/views/api/v1/show.json.rabl 
    code: object @vendor 
       attributes :id, :name 

    file: app/views/api/v1/index.json.rabl 
    code: object @vendors 
      attributes :id, :name 
8) Routes file looks like this 

api_v1_vendors GET /api/v1/vendors(.:format) api/v1/vendors#index {:format=>"json"} 

POST /api/v1/vendors(.:format)   api/v1/vendors#create {:format=>"json"} 

new_api_v1_vendor GET /api/v1/vendors/new(.:format)  api/v1/vendors#new {:format=>"json"} 

edit_api_v1_vendor GET /api/v1/vendors/:id/edit(.:format) api/v1/vendors#edit {:format=>"json"} 

api_v1_vendor GET /api/v1/vendors/:id(.:format)  api/v1/vendors#show {:format=>"json"} 
PUT /api/v1/vendors/:id(.:format)  api/v1/vendors#update {:format=>"json"} 

DELETE /api/v1/vendors/:id(.:format)  api/v1/vendors#destroy {:format=>"json"} 

9) Finally I went to url: "http://localhost:3000/api/v1/vendors.json" 

    And all I see in the browser is an empty JSON hash: "{}" 

很明显,它无法访问实例变量。似乎是超出范围的一些问题。我不知道如何继续下一步。我找不到任何与rabl版本相关的例子。有什么建议么?我真的很感激它

谢谢

回答

0

你在哪里设置实例变量?首先,你可能应该在你的Api :: ApplicationController(或者类似的东西,基本上是Api模块中的每个其他控制器继承的控制器)中都有这样一行:respond_to :json

你不需要这些:render 'api/v1/index.json.rabl'

只需设置你的实例变量:

def index 
    @vendors = Vendor.all 
    respond_with @vendors 
end 

def show 
    @vendor = Vendor.find(params[:id]) 
    respond_with @vendor 
end 
+0

谢谢您的回答。这工作。一个简单的问题:我应该如何处理我的控制器的html.erb文件和html视图的应用程序控制器?我应该保留它还是删除它。由于这是一个仅适用于API的应用程序 – banditKing

+0

如果只是API,我不认为有任何理由让它们在周围:) – Robin

相关问题