@亚历克斯·科金
@Sergio Tulentsev
它有点难看,但它有效(希望我在复制名称并将名称更改为更通用的名称时没有搞砸任何事情)。
config/routes.rb:
get 'loading_screen/:id' , to: 'model_name#loading_screen' , as: 'loading_screen'
match 'loading_status' , to: 'model_name#loading_status' , as: 'loading_status' , :via => [:get,:post]
在 model_name 控制器中:
def loading_screen
@model_name = ModelName.find_by(id: params.try(:fetch,:id,nil))
@id = @model_name.try(:id)
end
def loading_status
@model_name = ModelName.find_by(id: params.try(:fetch,:id,nil)
@id = @model_name.try(:id)
@json = { status: @model_name.try(:status) , id: @id }.to_json
respond_to do |format|
format.js do
render layout: false
end
end
end
查看 loading_screen.html.erb:
<script>
function checkStatus( id ) {
$.ajax({
url: '/loading_status',
data: { id: id } ,
dataType: 'json',
success: function(res) {
var status = res.status;
var id = res.id;
console.log("json = " + JSON.stringify(res,null,2) );
if ( status == "Done" ) {
window.location = "/path_to_view_completed_results/" + id;
myStopFunction();
} else if ( count > 30 ) {
myStopFunction();
}
}
});
}
var id = <%= @id %> ;
var count = 0;
var myInterval = setInterval(function(){ checkStatus( id ) }, 1000);
function myStopFunction() { clearInterval(myInterval); }
</script>
在 loading_status.js.erb 中:
<%= @json.html_safe %>