2014-10-28 45 views
2

我正在構建我的第一個應用程序,它使用javascript作爲對控制器操作請求的響應。我定義create動作像這樣我的控制器內:ActionController :: UnknownFormat同時在respond_to中使用JavaScript格式

def create 
    @cat = Cat.new(cat_params) 

    respond_to do |format| 
     if @cat.save 
     format.js 
     end 
    end 
end 

而且我已經創建create.js.erbviews/cats這對於響應代碼。我觸發我的觀點裏create行動後,我收到以下錯誤:

ActionController::UnknownFormat in CatsController#create 
ActionController::UnknownFormat 

如何處理這個問題呢?你有什麼建議?我正在使用Rails4

回答

0

如果@cat沒有保存,您可能會遇到這種錯誤,因爲您沒有格式。這應該適合你:

def create 
    @cat = Cat.new(cat_params) 

    respond_to do |format| 
    if @cat.save 
     format.js 
    else 
     #assuming that you are using js throughout 
     format.js { render :new } 
    end 
    end 
end 
+0

謝謝,但它不起作用。我仍然遇到麻煩。 – Bosquets 2014-10-30 01:47:28

相關問題