【问题标题】:Using versioning in a Rails API app- Cannot render JSON for a particular controller action在 Rails API 应用程序中使用版本控制 - 无法为特定控制器操作呈现 JSON
【发布时间】:2012-08-28 19:45:20
【问题描述】:

我创建了一个练习 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 进行版本控制的示例。有什么建议么?我真的很感激它

谢谢

【问题讨论】:

    标签: ruby-on-rails api versioning rabl


    【解决方案1】:

    你在哪里设置实例变量?

    首先,您可能应该在您的 Api::ApplicationController 中有这行:respond_to :json(或类似的东西,基本上是 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
    

    【讨论】:

    • 感谢您的回答。那行得通。一个简单的问题:我应该如何处理我的控制器的 html.erb 文件和 html 视图的应用程序控制器?我应该保留它还是删除它。由于这是一个仅限 API 的应用
    • 如果它只是 API,我认为没有任何理由保留它们 :)
    猜你喜欢
    • 2016-12-16
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多