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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 April 2011 15:27, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #991047:> On 5 April 2011 15:27, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 April 2011 16:07, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #991047: >> On 5 April 2011 15:27, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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, 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #991056:> On 5 April 2011 16:07, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 April 2011 17:58, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #991056: >> On 5 April 2011 16:07, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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?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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Edward Stembler
2011-Apr-05 17:12 UTC
Re: Re: Re: How to call a helper function from jQuery?
Colin Law wrote in post #991088:> On 5 April 2011 17:58, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 view AJAX after the page has already rendered. If it turns out to me too much effort, I just do it in the same cycle as the controller + view... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Apr-05 17:20 UTC
Re: Re: Re: Re: How to call a helper function from jQuery?
On 5 April 2011 18:12, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #991088: >> On 5 April 2011 17:58, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 view AJAX > after the page has already rendered. > > 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Edward Stembler
2011-Apr-05 18:17 UTC
Re: Re: Re: Re: How to call a helper function from jQuery?
Colin Law wrote in post #991091:> On 5 April 2011 18:12, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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: <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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Apr-05 19:51 UTC
Re: Re: Re: Re: Re: How to call a helper function from jQuery?
On 5 April 2011 19:17, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #991091: >> On 5 April 2011 18:12, Edward Stembler <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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: > > <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?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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.