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:
我真正想要的是:
- User click the button
- A JS modal window shows up and at the same time, trigger the rake task
- Rake task running till finish (modal window still open)
- Rake task finish →
redirect_to
is triggered
用户单击按钮
一个JS模态窗口显示,同时触发rake任务
耙任务运行直至完成(模态窗口仍然打开)
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:
你需要这样的东西:
-
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 * /})})
-
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%>
-
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:
你需要这样的东西:
-
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 * /})})
-
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%>
-
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