0

我创建了一个练习 rails 应用程序,我在其中创建了一个命名空间,并在此railscast中演示了版本。一切正常,我可以在浏览器中看到 json 输出

然后我添加了 Rabl gem 并试图渲染 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 进行版本控制的示例。有什么建议么?我真的很感激

谢谢

4

1 回答 1

0

你在哪里设置实例变量?

首先,您可能应该有这一行:respond_to :json在您的 Api::ApplicationController 中(或类似的东西,基本上是 Api 模块中所有其他控制器继承的控制器)。

你不需要这些: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
于 2012-08-28T20:02:57.070 回答