且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails在ajax请求上闪烁(不重新加载)

更新时间:2023-12-05 18:37:46

在Rails中,有一个gem可用于ajax调用Flash消息
烤面包机

In rails there is one gem for ajax call flash messages Toaster gem

application_helper.rb

def custom_bootstrap_flash
  flash_messages = []
  flash.each do |type, message|
    type = 'success' if type == 'notice'
    type = 'error'   if type == 'alert'
    text = "
      <script>
        $(function () {
          toastr.#{type}(\"#{message}\");
        });
      </script>
    "
    flash_messages << text.html_safe if message
  end
  flash_messages.join("\n").html_safe
end

将其包含在布局中 application.html.erb

<%= custom_bootstrap_flash %>

然后在您的 action.js.erb 使用烤面包机方法显示烤面包消息

And in your action.js.erb show toast message using toaster method

toastr.success('Success.')
toastr.error('error')

我希望这是您正在寻找的东西。

I hope this what you are looking.