javascript函数:
document.getElementById("pause").addEventListener("click", function(event){
event.preventDefault();
<% execution.pause %>
});
暂停按钮:
<%= button_tag(id:'pause', class: "btn btn-raised btn-primary") do %>
<span><i class="far fa-pause-circle"></i> Pause</span>
<% end %>
我有这个javascript函数,可在单击我的暂停按钮时进行监听。由于我每次加载页面时都会执行调用<% execution.pause %>
的行,因此无法正常工作。
我知道这是因为我不能完全像这样调用rails方法。而且,我是Rails的新手,我不知道如何仅在单击时调用和执行它。请给一些建议。
答案 0 :(得分:2)
<%= button_tag(id: 'pause', class: "btn btn-raised btn-primary") do %>
<span><i class="far fa-pause-circle"></i> Pause</span>
<% end %>
为继续执行提供路线
get '/execute/pause', to "custom#pause_execution"
在点击时触发ajax请求
<script type="text/javascript">
$('#pause').on('click', function(e){
e.preventDefault();
var execute_id = "<%=execution.id%>"
// ajax request
$.ajax({
url: "/execute/pause",
type: "GET",
data : {
id: execute_id
},
dataType: "script",
});
});
</script>
在custom_controller.rb
class CustomController < ApplicationController
def pause_execution
#assuming Execution is a model which has a pause method
execution = Execution.find(params[:id])
if execution.pause
flash[:notice] = "Paused successfully!"
else
flash[:error] = "Something went wrong"
end
end
end