Hi, I''ve searched for a while online and been unable to find anything concerning this. I have a controller with an index action, which has an index view. This view renders the index page but also needs to execute some javascript to show some data (using effects) when the page loads. I thought I could just create the view and RJS with the same name and the RJS would be run after the view, but it seems to stop if a view is found. Then I tried to render the RJS template in the controller, but that takes over and doesn''t show the view. Finally, I tried render :update and that also ignores rendering my html view. How can I render an html view for an action and then execute javascript (preferably with an RJS file) when the page loads? Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 17 Mar 2008, at 18:25, tparkin wrote:> > Hi, > > I''ve searched for a while online and been unable to find anything > concerning this. > > I have a controller with an index action, which has an index view. > This view renders the index page but also needs to execute some > javascript to show some data (using effects) when the page loads. I > thought I could just create the view and RJS with the same name and > the RJS would be run after the view, but it seems to stop if a view is > found. > > Then I tried to render the RJS template in the controller, but that > takes over and doesn''t show the view. Finally, I tried render :update > and that also ignores rendering my html view. > > How can I render an html view for an action and then execute > javascript (preferably with an RJS file) when the page loads?As you have found, there can only be one render. Not very nice option: with a script tag. Nicer option: have your index file load a separate js file that observes dom:loaded and fires off any stuff you need from there. rjs won''t help you much here (unless you generate the javascript on the fly), but to be honest I''ve come round to the feeling that if you''re generating javascript you might as well remove the middleman. Writing js with a library like prototype is actually quite fun. Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
At the bottom of your rhtml file you can use update_page_tag run generated javascript in a block that acts like an rjs file For example, ... ... <div id="hide"> Hide This Stuff </div> <br/> <%= update_page_tag do |page| page.alert("JavaScript Ran On Load!") page["hide"].hide end %> On Mar 17, 2:25 pm, tparkin <tpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''ve searched for a while online and been unable to find anything > concerning this. > > I have a controller with an index action, which has an index view. > This view renders the index page but also needs to execute some > javascript to show some data (using effects) when the page loads. I > thought I could just create the view and RJS with the same name and > the RJS would be run after the view, but it seems to stop if a view is > found. > > Then I tried to render the RJS template in the controller, but that > takes over and doesn''t show the view. Finally, I tried render :update > and that also ignores rendering my html view. > > How can I render an html view for an action and then execute > javascript (preferably with an RJS file) when the page loads? > > Thanks in advance.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Hi, Thanks for this ingenious solution! On 3/26/08, prooftheory <prooftheory-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > > At the bottom of your rhtml file you can use update_page_tag run > generated javascript in a block that acts like an rjs file > > For example, > > ... > ... > <div id="hide"> > Hide This Stuff > </div> > <br/> > <%= update_page_tag do |page| > page.alert("JavaScript Ran On Load!") > page["hide"].hide > end %> >-- Tim Parkin tparkin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Is it possible to stub out the included code, perhaps via a helper, and have it auto-included in the layout without actually modifying the page or layout? I''m trying to do something very similar to what Tim is, but I want it to eventually be a plugin and ideally it just appends the javascript stuff at the bottom of every page without the app developer having to do anything other than install the plugin (and maybe set an environment variable). -Dan On Mar 26, 2:51 pm, "Tim Parkin" <tpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Thanks for this ingenious solution! > > On 3/26/08, prooftheory <proofthe...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > ... > > <div id="hide"> > > Hide This Stuff > > </div> > > <br/> > > <%= update_page_tag do |page| > > page.alert("JavaScript Ran On Load!") > > page["hide"].hide > > end %>--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What if you actually want to use an rjs file? I have code that I want to reuse. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I suppose you can do the previously mentioned solution (read: hack) and do this: At the bottom of your rhtml file you can use update_page_tag run generated javascript in a block that acts like an rjs file For example, ... ... <div id="hide"> Hide This Stuff </div> <br/> <%= update_page_tag do |page| page.alert("JavaScript Ran On Load!") page["hide"].hide end %> But instead of doing an alert, do page << "myfunc();" and put myfunc() in your .js file so that you can reuse it. I haven''t heard of a way to do this with an actual .rjs file yet, but that would be the ideal solution. On 4/20/08, Sn Sc <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > What if you actually want to use an rjs file? I have code that I want to > reuse. >-- Tim Parkin tparkin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 20 Apr 2008, at 19:01, Tim Parkin wrote:> > But instead of doing an alert, do page << "myfunc();" and put > myfunc() in your .js file so that you can reuse it. I haven''t heard > of a way to do this with an actual .rjs file yet, but that would be > the ideal solution. >Don''t ask me if this is a good idea, but sticking <%= javascript_tag render( :partial => ''thejs'') %> <% controller.response.content_type = Mime::HTML%> in a view file seems to do the trick. The second line is needed because the rjs renderer sets the mime type to text/javascript (unless it is already set, so you may be able to do without it if you are in a respond_to block or something like that). Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, I''ll try that... does ''thejs'' refers to the file _thejs.rjs? I was thinking something more like this: http://onrails.org/articles/2006/01/12/rjs-and-link_to_function That would work, right? Any ideas to modify it so it just runs js on body load, not wrapped in a function? -S. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 20 Apr 2008, at 19:47, Sn Sc wrote:> > Thanks, I''ll try that... does ''thejs'' refers to the file _thejs.rjs? >Yup> I was thinking something more like this: > http://onrails.org/articles/2006/01/12/rjs-and-link_to_function > That would work, right? Any ideas to modify it so it just runs js on > body load, not wrapped in a function?It''s effectively the same as what I posted doing. Fred>--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---