Hello, I have an action in my controller called ''reply''. I would like to be able to select a particular RJS file to excute based on the results of some logic. In the simplest terms, this is what I''d like to accomplish: def reply if case == a [use reply_a.rjs] else [use reply_b.rjs] end end Now Rails has a convention that says, "if no render statement is defined, look for and .rhtml or .rjs file to execute based on the name of the action; i.e reply.rjs. I can seem to find a particluar command that will allow me to specify which RJS to use. It seems that such a command should exist, but I am to bone-headed to find it. (Also, just curious, does anyone know of a way to do logic inside an RJS template?) -- 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 -~----------~----~----~----~------~----~------~--~---
Elliott Blatt wrote:> I would like to be > able to select a particular RJS file to excute based on the results of > some logic. > > In the simplest terms, this is what I''d like to accomplish: > > def reply > if case == a > [use reply_a.rjs] > else > [use reply_b.rjs] > end > end > > Now Rails has a convention that says, "if no render statement is > defined, look for and .rhtml or .rjs file to execute based on the name > of the action; i.e reply.rjs. I can seem to find a particluar command > that will allow me to specify which RJS to use.The clue is in "if no render statement is defined..." ;) You need to use the ''render'' method, and in particular the :action option: render(:action => :reply_a) or render(:action => :reply_b) The documentation''s here: http://api.rubyonrails.org/classes/ActionController/Base.html#M000206> (Also, just curious, does anyone know of a way to do logic inside an RJS > template?)An RJS template is just pure Ruby, so you can use the usual if/elsif/end etc. Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> You need to use the ''render'' method, and in particular the :action > option: > > render(:action => :reply_a) > > or > > render(:action => :reply_b) > > The documentation''s here: > > http://api.rubyonrails.org/classes/ActionController/Base.html#M000206 > >> (Also, just curious, does anyone know of a way to do logic inside an RJS >> template?) > > An RJS template is just pure Ruby, so you can use the usual > if/elsif/end etc. > > ChrisThanks, well done. I had tried, render :parial and render :template. I didn''t think of render :action for some reason. -- 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 -~----------~----~----~----~------~----~------~--~---
Elliott Blatt wrote:> Thanks, well done. I had tried, render :parial and render :template. I > didn''t think of render :action for some reason.Yeah, the :action option is a bit weirdly named. It makes you think that it''s going to execute the action you specify, but actually it just renders the template by that name. When I write render(:action => :wibble), I like to think of it as telling Rails to "pretend that I''m in an action called ''wibble'', and go and find the default template for that action, following the normal default rules". That''s why it works equally well for RHTML/RJS/whatever templates without specifying the extension, because Rails'' default behaviour is to find one of those templates given just the name of the action. Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> >> (Also, just curious, does anyone know of a way to do logic inside an RJS >> template?) > > An RJS template is just pure Ruby, so you can use the usual > if/elsif/end etc. >...the if/else/etc/ works, but how do you pass parameteres to the RJS template? all of the global variables that are in the action are passed to the template? (@user, @page, @var) ? i can''t seem to get a variable into the RJS template ... -- 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 -~----------~----~----~----~------~----~------~--~---
harp wrote:> how do you pass parameteres to the RJS > template? all of the global variables that are in the action are passed > to the template? (@user, @page, @var) ? i can''t seem to get a variable > into the RJS template ...Yeah, that should work. If you do @user = User.find(:first) in your controller, your RJS template should be able to access that instance variable as well: page.alert(@user.name) Same as any template. Can you post your code that''s not working as expected? Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---