I have a scenario where after updating a section on a page with a partial, I then need to update that same area with a new partial. This is easy, however, at this stage I need to display partial A if the user is logged in, and B if the user is not logged in. How do I programmatically decide which partial to render? -- 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 -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2007-Apr-05 10:03 UTC
Re: How to: programmatically choose a partial to render
On Apr 5, 2007, at 05:28 , Arch Stanton wrote:> I have a scenario where after updating a section on a page with a > partial, I then need to update that same area with a new partial. > This > is easy, however, at this stage I need to display partial A if the > user > is logged in, and B if the user is not logged in. > > How do I programmatically decide which partial to render?render(:partial => (user.logged_in? ? ''foo'' : ''bar'')) and there''s also some more readable but more verbose variants, I imagine. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Arch Stanton
2007-Apr-05 17:35 UTC
Re: How to: programmatically choose a partial to render
Jakob Skjerning wrote:> On Apr 5, 2007, at 05:28 , Arch Stanton wrote: > >> I have a scenario where after updating a section on a page with a >> partial, I then need to update that same area with a new partial. >> This >> is easy, however, at this stage I need to display partial A if the >> user >> is logged in, and B if the user is not logged in. >> >> How do I programmatically decide which partial to render? > > render(:partial => (user.logged_in? ? ''foo'' : ''bar'')) > > and there''s also some more readable but more verbose variants, I > imagine. > > -- > Jakob Skjerning - http://mentalized.netI have the action "logged_in?" defined in my User class, but when I call user.logged_in? I get an error: ActionView::TemplateError (undefined method `logged_in?'' for User:Class) -- 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 -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2007-Apr-05 18:55 UTC
Re: How to: programmatically choose a partial to render
Arch Stanton wrote:> I have the action "logged_in?" defined in my User class, but when I call > user.logged_in? I get an error: > > ActionView::TemplateError (undefined method `logged_in?'' for User:Class)So the question really is "How to check if a user is logged in"? It looks like you''re trying to call logged_in? on the User class. For that to work your logged_in? method needs to be a class method. Ie it has to be defined with def self.logged_in? or something to that effect. http://www.rubycentral.com/book/tut_classes.html has more information. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Arch Stanton
2007-Apr-05 19:41 UTC
Re: How to: programmatically choose a partial to render
Jakob Skjerning wrote:> Arch Stanton wrote: >> I have the action "logged_in?" defined in my User class, but when I call >> user.logged_in? I get an error: >> >> ActionView::TemplateError (undefined method `logged_in?'' for User:Class) > > So the question really is "How to check if a user is logged in"? It > looks like you''re trying to call logged_in? on the User class. For that > to work your logged_in? method needs to be a class method. Ie it has to > be defined with def self.logged_in? or something to that effect. > > http://www.rubycentral.com/book/tut_classes.html has more information. > > -- > Jakob Skjerning - http://mentalized.netYa this question has opened a whole can of worms actually ;) I did create a class method, but I made the mistake of checking the session, which the model does not have access to. So I''m trying to rethink the entire problem. What is the better route here: Create a class method User.logged_in? and pass the session, or create a controller action . . . but then I wonder, can I call a controller action from the template. Still trying to understand the framework. -- 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 am specifying layout based on login, which is similar to what you are doing... I have this method in my ApplicationController def logged_in_user @logged_in_user ||= User.find session[:user] end To check if someone is logged in I write "if logged_in_user" but you can add a method to ApplicationController like this... def logged_in? logged_in_user ? true : false end On Apr 6, 7:41 am, Arch Stanton <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Jakob Skjerning wrote: > > Arch Stanton wrote: > >> I have the action "logged_in?" defined in my User class, but when I call > >> user.logged_in? I get an error: > > >> ActionView::TemplateError (undefined method `logged_in?'' for User:Class) > > > So the question really is "How to check if a user is logged in"? It > > looks like you''re trying to call logged_in? on the User class. For that > > to work your logged_in? method needs to be a class method. Ie it has to > > be defined with def self.logged_in? or something to that effect. > > >http://www.rubycentral.com/book/tut_classes.htmlhas more information. > > > -- > > Jakob Skjerning -http://mentalized.net > > Ya this question has opened a whole can of worms actually ;) > > I did create a class method, but I made the mistake of checking the > session, which the model does not have access to. > > So I''m trying to rethink the entire problem. > > What is the better route here: > > Create a class method User.logged_in? and pass the session, or create a > controller action . . . but then I wonder, can I call a controller > action from the template. Still trying to understand the framework. > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Arch Stanton
2007-Apr-05 22:21 UTC
Re: How to: programmatically choose a partial to render
reed wrote:> I am specifying layout based on login, which is similar to what you > are doing... > > I have this method in my ApplicationController > def logged_in_user > @logged_in_user ||= User.find session[:user] > end > > To check if someone is logged in I write "if logged_in_user" but you > can add a method to ApplicationController like this... > > def logged_in? > logged_in_user ? true : false > end > > On Apr 6, 7:41 am, Arch Stanton <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>I must have some confusion around how to access controller methods from a view. I''m calling 4: <% if logged_in? -%> From an rhtml, but I get the error: ActionView::TemplateError (undefined method `logged_in?'' for #<#<Class:0x48d2c60>:0x48d2c38>) I have that method defined in my Application Controller. -- 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 -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2007-Apr-07 10:34 UTC
Re: How to: programmatically choose a partial to render
On Apr 6, 2007, at 00:21 , Arch Stanton wrote:> From an rhtml, but I get the error: ActionView::TemplateError > (undefined > method `logged_in?'' for #<#<Class:0x48d2c60>:0x48d2c38>) > > I have that method defined in my Application Controller.You need to expose the controller method to your view by using helper_method <http://api.rubyonrails.org/classes/ActionController/ Helpers/ClassMethods.html#M000165>. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---