I have a Rails 3 application that performs a long task of making several queries to a database to find documents. I want to show the user the progress of through the set of queries. I created a jquery function that employs the jquery.ui.progressbar. I want to update the progress bar dynamically as the document search controller loops through the search phrases. I need to know how to dynamically call the javascript to update the progressbar div in the document view. Please let me know how to call the javascript from the rails contr开发者_如何学Gooller.
You can't call a remote function from your server unless it is responding to a request so would need to frequently send requests to your server from your application, check the progress of the search, and respond accordingly. Unless you're talking about more than 10 seconds I really would go for a spinner as it will be a lot easier and just as intuitive.
Edit:
examples of how you may update your page in response to ajax polling
#some_controller.rb
def check_download_progress
#some_method
end
# javascript erb file. views/some_controller/check_download_progress.js.erb
# You can mix js and ruby and send it as a response to an ajax call; examples:
#check_download_progress.js.erb
<%- if @some_value.present? -%>
$('#progress_bar').addClass('<%= @some_value -%>');
<%- end -%>
#or simply call a function
my_remote_function(<%= @some_value -%>);
In your Rails action you could use RJS:
# Your AJAX action in controller.
def update_progress_bar
render :update do |page|
page.call 'java_script_method_name', 'param1', 'param2', etc.
end
end
精彩评论