没有将Symbol隐式转换为String rails

时间:2016-05-17 08:48:26

标签: ruby-on-rails

在模型日历中删除/创建记录时出现问题,但仅在我使用flash[:alert] = "Notification deleted"时才出现问题。它只发生在这个模型中。基本上如果我使用

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

一切正常,但如果我在flash[:alert] = "Notification deleted"之后添加redirect_to,就像这样:

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]并且它正在工作,但是这个有错误。

我不知道如何进一步跟踪错误。

1 个答案:

答案 0 :(得分:5)

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