How to: programmatically choose a partial to render

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?

On Apr 5, 2007, at 05:28 , Arch S. 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 S. - http://mentalized.net

Jakob S. wrote:

On Apr 5, 2007, at 05:28 , Arch S. 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 S. - http://mentalized.net

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)

Arch S. 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 S. - http://mentalized.net

Jakob S. wrote:

Arch S. 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 S. - http://mentalized.net

Ya this question has opened a whole can of worms actually :wink:

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.

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 S. [email protected]

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.

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 S. [email protected]

On Apr 6, 2007, at 00:21 , Arch S. 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 S. - http://mentalized.net