如何在重定向发生之前在Rails rake任务期间显示JS模态窗口?

时间:2023-01-19 22:02:25

I've this inside my orders.html.erb

我在orders.html.erb里面这个

<%= link_to "Create Next Week Orders", create_next_week_orders_path, class: "btn btn-primary" %>

<%= link_to“创建下周订单”,create_next_week_orders_path,类:“btn btn-primary”%>

That points to this method inside static_pages_controller.rb

这指向static_pages_controller.rb中的这个方法

def create_next_week_orders
    %x(bundle exec rake create_orders_next_week)
    redirect_to root_path
end

What happened here is, when user click the button, the rake task will be running and it takes some seconds to finish before the redirect_to is triggered.

这里发生的是,当用户单击按钮时,rake任务将运行,并且在redirect_to被触发之前需要几秒钟才能完成。

What I actually want is:

我真正想要的是:

  1. User click the button
  2. 用户单击按钮

  3. A JS modal window shows up and at the same time, trigger the rake task
  4. 一个JS模态窗口显示,同时触发rake任务

  5. Rake task running till finish (modal window still open)
  6. 耙任务运行直至完成(模态窗口仍然打开)

  7. Rake task finish → redirect_to is triggered
  8. Rake任务完成→redirect_to被触发

How do I achieve this flow?

我如何实现这一流程?

Is it possible to make it like this?

有可能像这样吗?

def create_next_week_orders
    respond_to :js
    %x(bundle exec rake create_orders_next_week)
    respond_to :html
end

My routes.rb

get "create_next_week_orders"     => "static_pages#create_next_week_orders"

1 个解决方案

#1


0  

You need something like this:

你需要这样的东西:

  1. You need to add click event handler to your button that will open the modal

    您需要将click事件处理程序添加到将打开模态的按钮

    $(function(){ $(".your-button").click(function(){ /* code to show modal */ }) })

    $(function(){$(“。your-button”)。click(function(){/ * code to show modal * /})})

  2. And set remote option as true in your button

    并在按钮中将远程选项设置为true

    in your view: <%= link_to "Your Button", path_to_action, class: ".your-button", remote: true %>

    在您的视图中:<%= link_to“Your Button”,path_to_action,class:“。yourour-button”,remote:true%>

  3. Then in the controller action:

    然后在控制器动作中:

    def create_next_week_orders %x(bundle exec rake create_orders_next_week) render :js => "window.location('#{root_path}')" end

    def create_next_week_orders%x(bundle exec rake create_orders_next_week)render:js =>“window.location('#{root_path}')”end

#1


0  

You need something like this:

你需要这样的东西:

  1. You need to add click event handler to your button that will open the modal

    您需要将click事件处理程序添加到将打开模态的按钮

    $(function(){ $(".your-button").click(function(){ /* code to show modal */ }) })

    $(function(){$(“。your-button”)。click(function(){/ * code to show modal * /})})

  2. And set remote option as true in your button

    并在按钮中将远程选项设置为true

    in your view: <%= link_to "Your Button", path_to_action, class: ".your-button", remote: true %>

    在您的视图中:<%= link_to“Your Button”,path_to_action,class:“。yourour-button”,remote:true%>

  3. Then in the controller action:

    然后在控制器动作中:

    def create_next_week_orders %x(bundle exec rake create_orders_next_week) render :js => "window.location('#{root_path}')" end

    def create_next_week_orders%x(bundle exec rake create_orders_next_week)render:js =>“window.location('#{root_path}')”end