2012-08-28 19 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

相關問題