2011-12-01 30 views
3

我有一個comment資源。我有一個處理respond_with的控制器,現在它應該可以提供所有JSON響應(正確發生)。我使用Rabl來處理我的JSON/XML渲染,而且我正在幹一些事情。我有正確的方式我想要一個comment呈現在comments/show.rablRails 3 respond_with:在資源創建時顯示「show」佈局

object @comment 

attributes :id, :body, :a_few_more_things 

當POST呼叫在/comments/製成(其將觸發create方法我的控制器上),我想的Rails在同一格式返回commentshow視圖(上圖)。我有,在我的create功能...

def create 
    # Skip some code, save it, ya-da ya-da 
    respond_with(@comment, :layout => 'comments/show') 
end 

這是行不通的;它只是返回一個帶有所有屬性的comment的扁平JSON實現。它沒有使用我的show.rablcomments/show.rabl如何使用show.rabl作爲佈局返回@comment的動作create

我看到this post指定佈局文件的完整路徑和擴展名;我不應該這樣做,我應該嗎?我使用錯了:symbol_option?應該是:location

+1

採取在[JBuilder的頂(https://github.com/rails/ JBuilder中) – Damien

回答

3

我能夠通過在comments/create.rabl上創建Rabl模板來解決此問題。

object @comment 

extends "comments/show" 

這就是她寫的全部內容。 Rails查找create.rabl視圖,該視圖接受一個對象並僅呈現comments/show.rabl中定義的字段。

感謝Martin Harrigan提醒我我仍然有我的問題打開!

0

您可以使用,以保持乾爽:

render :show, :status => :created 
0

在Rails4您可以指定模板:

def create 
    respond_with @comment, status: :created, template: 'comments/show' 
end