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?

On 5 April 2011 15:27, Edward S. [email protected] wrote:

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 L. wrote in post #991047:

On 5 April 2011 15:27, Edward S. [email protected] wrote:
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.

On 5 April 2011 16:07, Edward S. [email protected] wrote:

after document.ready fire off AJAX calls to replace the spinners with a
count of all of the records in each table listed.

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 L. wrote in post #991056:

On 5 April 2011 16:07, Edward S. [email protected] wrote:
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.

Yes, I’m not sure what the proper procedure is, or the best practice.
If I cannot make the call from javascript to my helper function, then do
I create a .js.erb partial view? And if so, what does the pattern look
like?

Colin L. wrote in post #991088:

On 5 April 2011 17:58, Edward S. [email protected] wrote:
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.

True. I could just let the Rails controller + view render the table
counts. Though I didn’t want to slow down the rendering of the page for
the table counts. That’s why I’m investigating how to do it using AJAX
after the page has already rendered.

If it turns out to be too much effort, I’ll just do it in the same cycle
as the controller + view…

On 5 April 2011 18:12, Edward S. [email protected] wrote:

If it turns out to me too much effort, I just do it in the same cycle as
the controller + view…

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

On 5 April 2011 17:58, Edward S. [email protected] wrote:

like?
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

On 5 April 2011 19:17, Edward S. [email protected] wrote:

view:

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?

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

Colin L. wrote in post #991091:

On 5 April 2011 18:12, Edward S. [email protected] wrote:
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:

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