How to call a helper function from jQuery?

I have a helper function which accepts a table_name parameter and outputs html. How (and from where) do I call that Rails function from Javascript (or jQuery)?

Can I call it from jQuery's document.ready? Or, do I need a .js.erb partial? Or, am I missing something else completely?

Is it a ruby helper function? If so you can only run it on the server before rendering the page (or ajax response), so you cannot call it from javascript. What are you trying to achieve?

Colin

Colin Law wrote in post #991047:

Is it a ruby helper function? If so you can only run it on the server before rendering the page (or ajax response), so you cannot call it from javascript. What are you trying to achieve?

It's a custom helper function, I defined in a module in app/helpers (Rails 3 app).

My goal, is to list several table names with spinners after them. Then, after document.ready fire off AJAX calls to replace the spinners with a count of all of the records in each table listed.

So, something like this:

Customer Orders *

becomes this:

Customer Orders (125)

If you imagine the asterisk is a spinner.

So, if I understand correctly, you don't want to call the helper from javascript, you want to fire the AJAX call from javascript and call the helper from the view rendered by the AJAX action on the server, to update the appropriate div on the page.

Colin

Colin Law wrote in post #991056:

The helper is a ruby file returning html, right? In that case you just call it from the html.erb file rendered by the AJAX action. js doesn't come into it.

Colin

Colin Law wrote in post #991088:

I don't think you read my post carefully, to repeat, call the helper from the html.erb file rendered by the *AJAX* action.

Colin

Colin Law wrote in post #991091:

I don't think you read my post carefully, to repeat, call the helper from the html.erb file rendered by the *AJAX* action.

Yes, I'm not quite understanding what you're suggesting.

I can see this sort of working one way:

In my view, have a script block where jQuery calls the corresponding js view:

<script type='text/javascript'>     $(document).ready(function(){          $.getScript('<%= project_path(@project, :js) %>');     }); </script>

which calls show.js.erb:

$("#spinner").html("<%= escape_javascript( my_helper_func )) %>");

which calls my_helper_func. This all seems a little like an overkill to me. Plus, I'm not sure how to pass along the table_name parameter.

Are you suggesting something simpler? Do you mind posting some psuedo-code?

Thanks

I was just imagining a simple ajax call returning html (not js) which replaces the contents of the div containing the spinner with the value required.

Colin