2016-05-17 56 views
0

我在刪除/創建模型日曆中的記錄時遇到問題,但僅在使用flash[:alert] = "Notification deleted"時出現問題。它只在這個模型中發生。基本上,如果我用沒有將Symbol隱式轉換爲String rails

def destroy 
     if @calendar.destroy 
     redirect_to calendars_path 
     else 
     redirect_to :back, :flash => { :error => "Failed to delete!" } 
     end 
    end 

一切工作正常,但如果我的redirect_to這樣之後添加flash[:alert] = "Notification deleted"

def destroy 
     if @calendar.destroy 
     redirect_to calendars_path, flash[:alert] = "Notification deleted" 
     else 
     redirect_to :back, :flash => { :error => "Failed to delete!" } 
     end 
    end 

我得到TypeError in CalendarsController#destroy。我在許多控制器中使用flash [:alert],並且它正在工作,但是這個錯誤。

我不知道如何進一步跟蹤錯誤。

回答

4

flash[:alert] = "Notification deleted"將返回字符串。這意味着它運行時會變成

redirect_to calendars_path, "Notification deleted" 

根據docs,這是無效的。除第一個以外的所有參數都必須是關鍵值。

更改爲

def destroy 
    if @calendar.destroy 
    redirect_to calendars_path, flash: { alert: "Notification deleted" } 
    # You can omit the flash key as well 
    # redirect_to calendars_path, alert: "Notification deleted" 
    else 
    redirect_to :back, :flash => { :error => "Failed to delete!" } 
    end 
end 

或者重定向之前分配移動到。

def destroy 
    if @calendar.destroy 
    flash[:alert] = "Notification deleted" 
    redirect_to calendars_path 
    else 
    redirect_to :back, :flash => { :error => "Failed to delete!" } 
    end 
end 
+0

謝謝。現在它正在工作。不知道我是如何錯過的。我在其他控制器中擁有相同的(正確的方式),但是現在它發生了,你能解釋爲什麼它是這樣工作的,而不是我在這裏嘗試過的嗎? –

相關問題