没有将Symbol隐式转换为String rails

Bog*_*iel 0 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
Run Code Online (Sandbox Code Playgroud)

一切都很好,但如果我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
Run Code Online (Sandbox Code Playgroud)

我得到 TypeError in CalendarsController#destroy.我在许多控制器中使用flash [:alert]并且它正在工作,但是这个有错误.

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

Dan*_*son 5

flash[:alert] = "Notification deleted"将返回字符串.这意味着当它运行时它将变成

redirect_to calendars_path, "Notification deleted"
Run Code Online (Sandbox Code Playgroud)

根据文档,这是无效的.除第一个之外的所有参数都必须是键值.

改成

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
Run Code Online (Sandbox Code Playgroud)

或者在重定向之前将分配移动到.

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