2013-02-07 51 views
7

我使用rails-apirabl gem來製作api應用程序。我不想將很多邏輯放入rabl視圖中,但是如果我想將其提取到幫助器中。 Rails的API寶石不包括助手的支持(和多數民衆OK),所以我怎麼可以讓助手到我的JSON的看法?(我需要什麼樣的中間件包括哪些模塊等)如何使用視圖助手與rails-api gem?

回答

11

在呈現的控制器查看您必須

  1. 包括ActionController的::助手和
  2. 提供使用「助手」方法

自己的助手模塊的觀點在我的API應用程序我創建了一個RenderingControll呃,它可以作爲所有控制器的超類:

class RenderingController < ApplicationController 
    include AbstractController::Layouts 
    include AbstractController::Translation 
    include ActionController::ImplicitRender 
    include ActionController::Helpers 

    helper ApplicationHelper, OtherHelper 
end 
+1

絕對正確,但我將它包含在'''ApplicationController'''中,因爲我想在所有視圖中使用我的助手。 – freemanoid

+0

難道你不能只擴展ActionController :: Base嗎? – tonyhb

+0

你可以,但你會包括一堆你可能不需要的模塊。 –