2016-09-22 43 views
0

我有一個與反應集成的Rails應用程序,我的視圖呈現與反應,包括html(JSX)。我注意到,當我的視圖是常規的erb視圖時,我有幾個format.html響應,現在它們不是,我仍然應該對html作出響應以防萬一(即使我看不到用戶如何使用我的應用程序if他們的JavaScript已禁用)?如果view使用javascript呈現,我還應該對html做出迴應嗎?

例子:

def destroy 
    @comment.destroy 
    respond_to do |format| 
     format.json { head :no_content } 
     format.html { redirect_to @question, notice: 'Comment was deleted.' } 
    end 
    end 

我可以擺脫HTML響應的?

回答

1

是否保留是否是個人選擇。我有時候會這麼做,但是更少的LOC使得代碼更簡潔。要刪除它,你有幾個選項。您可以將respond_to原樣,只是刪除HTML如:

def destroy 
    @comment.destroy 
    respond_to do |format| 
    format.json { head :no_content } 
    end 
end 

但你也可以從每個操作(甚至更少的LOC),像這樣的東西刪除respond_to

# put this LOC at the top of your controller, outside of any action 
respond_with :json 

# then each action is much simpler... you just assume it's always json 
def destroy 
    @comment.destroy 
    head :no_content 
end 
+1

大,所以這是一種個人選擇,有時候我會對這些簡單的選擇產生偏執,並對此做出重大的處理。謝謝Taryn –