2013-07-30 50 views
0

我想知道如何用Rabl渲染只需 html。我有一個沒有對象的html部分,到目前爲止我只能看到部分對象的例子。如果我在沒有對象的情況下嘗試它(顯然)會拋出錯誤。用Rabl渲染html只需要

我得到的最接近是:

node(:content) do 
    partial("api/v1/api/partials/tips") 
end 

Here is the documentation for Rabl

UPDATE

我最終只是這下面去。明顯的權利?出於某種原因,當我第一次嘗試時它不起作用。所以對於只在API響應中發送HTML,這很好。

def tips 
    render partial: "api/v1/api/partials/tips" 
    end 
+1

「用於生成JSON,XML,MessagePack,PList和BSON的系統」。 - 它沒有提到生成HTML的事情。爲什麼不使用常規ERB? –

+0

部分是erb。我需要發送HTML到iOS應用程序,我打開更好的想法:)。感謝您的輸入。 – Dol

回答

0

這可以通過渲染Rabl的節點內的ERB模板來實現:

object @user 

node(:html_content) do |user| 
    @user = user 
    template = Rails.root.to_s + '/app/views/api/users/show.html.erb' 
    ERB.new(File.read(template)).result(self.binding) 
end 
3
在Rabl的視圖

,例如show.v1.rabl你能夠呈現局部有以下視圖(rails 4.1.2 ):

object @your_object 

code :html do 
    context_scope.controller.render_to_string(

    # from app/views/ 
    partial: 'path/to/show.html.erb', 

    # locals (if needed) 
    locals: { 
     your_object: @your_object 
    } 

) 
end