【问题标题】:Calling flash notification via JS通过 JS 调用 flash 通知
【发布时间】:2016-08-01 20:08:26
【问题描述】:

基础 Rails 应用程序具有可通过以下方式调用的 Flash 消息:

"flash.now[:error]"

但是,如何通过 JS 手动调用呢?这是我当前的代码:

var show_error = function (message) {
  $("#flash-messages").html('<div class="flashes"> <div class="flash-notice">' + message + '</div> </div>');
  return false;
};

【问题讨论】:

  • 在我看来你有一个可以这样调用的函数:show_error('Something is Flasing!') 还是我误解了这个问题?

标签: javascript ruby-on-rails ruby-on-rails-5


【解决方案1】:

您可以通过将其包含在函数中来设置默认消息。在此处阅读有关 JS || 的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR_()

您可能还想看到这个答案,所以:Best practice method of displaying flash messages

var show_error = function (message) {
  message = message || 'Something should be flashing here!';
  $("#flash-messages").html('<div class="flashes"> <div class="flash-notice">' + message + '</div> </div>');
  return false;
};

/*
show_error();
*/

/*
show_error('Something is Flashing!');
*/

【讨论】:

  • 没用。我正在尝试将其设置为 true:“”。
  • 您询问了有关设置默认值的问题。请改写您的问题以获得更准确的答案。
  • 我询问了如何在 JS 中调用 Flash 消息 - 与“flash.now[:error]”调用的消息相同。不过感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-09-07
  • 2021-09-05
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多