Ruby on rails Rails 5如何处理显示flash成功/错误消息的响应,如果表单随remote true一起提交? Rails 5如何处理显示flash成功/错误消息的响应, 如果远程提交的表单为true

Ruby on rails Rails 5如何处理显示flash成功/错误消息的响应,如果表单随remote true一起提交? Rails 5如何处理显示flash成功/错误消息的响应, 如果远程提交的表单为true,ruby-on-rails,Ruby On Rails,在视图中添加一个分部,我建议创建一个视图/共享文件夹 app/views/shared/_flash.html.erb Remote true使rails使用ajax提交表单,因此您可以在前端处理请求 $(document).ready(() => function showFlash(message, color) { // Handle finding your flash // clear it, set color and message } $("#y

在视图中添加一个分部,我建议创建一个视图/共享文件夹

app/views/shared/_flash.html.erb


Remote true使rails使用ajax提交表单,因此您可以在前端处理请求

$(document).ready(() =>
  function showFlash(message, color) {
    // Handle finding your flash
    // clear it, set color and message
  }
  $("#your_selector").on("ajax:success", function(event) {
    const [data, status, xhr] = Array.from(event.detail);
    showFlash(xhr.responseText, 'green');
  }).on("ajax:error", function(event) {
    showFlash('Sorry there was an error', 'green')
  })
);
有了以上这些,你可以得到更多的幻想,但至少应该给你的骨骼工作

<%= render 'shared/flash' %>
$('#flash-messages').html("<%= j render 'shared/flash' %>");
flash.now[:notice] = 'Request was saved successfully.'
$(document).ready(() =>
  function showFlash(message, color) {
    // Handle finding your flash
    // clear it, set color and message
  }
  $("#your_selector").on("ajax:success", function(event) {
    const [data, status, xhr] = Array.from(event.detail);
    showFlash(xhr.responseText, 'green');
  }).on("ajax:error", function(event) {
    showFlash('Sorry there was an error', 'green')
  })
);