2012-07-13 48 views
1

有一些情況下,我需要在兩者使用的Rails時respond_with有一個模板,並返回錯誤代碼3Respond_with模板和狀態碼

我有一個前過濾器,如下所示:

def ensure_premium 
    respond_with("Must be a premium user!", status: 401, location: nil) unless current_user.is_premium? 
end 

和創造的行動,執行以下操作:

def create 
    @wait_list = @hangout.wait_lists.find_or_create_by(user_id: current_user.id) 
    respond_with(@wait_list) do |format| 
     format.json {render 'create', status: 201} 
    end 
end 

即使過濾旅行之前,它仍然會嘗試渲染這將導致一個錯誤的模板。我錯過了什麼讓它返回正確的錯誤和狀態碼,而不是渲染模板?

回答

0

我最終會完全不同的路線,所以這個問題不再有效。

0

您有多個respond_with創建操作。但我覺得更重要的是,你可能需要:

def ensure_premium 
    respond_with :json => {:error => "Must be a premium user!", :status => :unauthorized } unless.... 

我不認爲這是問題,但要確保在你的控制器,你有

class SomeController < ApplicationController 
    respond_to :json 
+0

我確實在控制器中有respond_to:json,但今天我會嘗試一下你的建議,看看它是否有幫助。 – 2012-07-17 13:36:04

+0

不幸的是,這最終沒有幫助。它仍然嘗試渲染模板 – 2012-07-19 12:45:16