Acts As Authenticated - smarter login box

Hi,

I’ve got AAA loaded and working. I now want to put a smarter login form
on the sidebar of my site.

If the user is not logged in, I’d like to display a typical form in the
sidebar that collects the user ID/password and allows the user to login.

If the user is logged in, the box should display something like ‘Logged
in user: FirstName Lastname’ and a button to allow the user to log out.

I’m new at rails, so I’m confused about how to go about doing this.

The main site template is in app/views/layouts/application.rb, and I’ve
tried modifying it as:

<% if [ logged in ] %>
<%= render(:partial => ‘account/logout’ %>
<% else %>
<%= render(:partial => ‘account/login’ %>
<% end %>

With all sorts of unsuccessful guesses for the [ logged in ] portion. I
can’t figure out how to tell if anyone is logged in without the page
throwing exceptions. Is there any way to have the view dump out all
available instance variables, or at least a list of them?

Also, it’s not DRY, as the login form is actually already located in
app/views/account/login.rhtml. In order to use the partials, I have to
copy the form to app/views/account/_login.rhtml.

Ideally, it would be great if I could just have one line of code in the
main view like
<%= render_the_login_form %> and the code would be smart enough to show
the proper form based on the logged in state of the user.

I get the feeling that I’m not going about this in a very rails-like
way.

Advice appreciated. Thanks!

Regards,
Rich

On 7/13/06, Duzenbury, Rich [email protected] wrote:

<%= render(:partial => ‘account/logout’ %>
<% else %>
<%= render(:partial => ‘account/login’ %>
<% end %>

Did you try “if logged_in?” ?

Duzenbury, Rich wrote:

in user: FirstName Lastname’ and a button to allow the user to log out.
<% end %>

Try:
if current_user (which will be nil if user is not logged in, will return
the user’s id otherwise)

On 7/13/06, Duzenbury, Rich [email protected] wrote:
Hi,

If the user is logged in, the box should display something like ‘Logged
in user: FirstName Lastname’ and a button to allow the user to log out.

The main site template is in app/views/layouts/application.rb, and I’ve
tried modifying it as:

<% if [ logged in ] %>
<%= render(:partial => ‘account/logout’ %>
<% else %>
<%= render(:partial => ‘account/login’ %>
<% end %>

Did you try “if logged_in?” ?

Yes! Can I call a method like logged in from the view?

What happens is that quite often, not sure exactly why, logged_in
returns a true value, causing the logout partial view to be invoked.
However, the partial crashes because @current_user is nil.

I’ll post some specific code in the morning.

Thanks!

Regards,
Rich

logged_in? and current_user are mixed into the view so you should be
able
to call them. If you’re using acts_as_authenticated unmodified, there’s
a
difference between logged_in? and current_user:

logged_in?: check for session[:user] which is just a user_id
current_user: load the User object from db based on session[:user]

I saw a similar problem when using edge rails yesterday. Not sure what
caused it though.

Hi,
try this code for login/logout. It worked for me…

<%if @session[‘user’].nil?%>
<%= link_to (“Login”, :controller=>“xyz”, :action =>
:login)%>
<%else%>
<%= link_to( “Logout”, :controller=>“xyz”, :action =>
:logout)%>
<%end%>

~Harish

On 7/13/06, Cuong T. [email protected] wrote:

Regards,
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Also, maybe a typo, but you’ll need to close the brackets on your partial
calls

Duzenbury, Rich wrote on 13.07.2006 22:28:

<% if [ logged in ] %>
<%= render(:partial => ‘account/logout’ %>
<% else %>
<%= render(:partial => ‘account/login’ %>
<% end %>

Try this…

<% if logged_in? -%>
<%= render :partial => ‘account/logout’ -%>
<% else -%>
<%= render :partial => ‘account/login’ -%>
<% end -%>

and you’ll need a partial _logout.rhtml and _login.rhtml in
views/account/ directory!

views/account/ directory!

Yes, I had tried logged_in? Sometimes logged_in? would return true, and
then the partial ‘logout’ would crash. I was not able to figure out why
logged_in? might return true, even if not logged in. I’ll just chalk
that up to inexperience. It seems to work fine now, at any rate.

The reason why the partial would crash is that I was trying to use
@current_user instance variable, which is nil when a new session starts.

In reality, I wanted to use the method/subroutine current_user of
lib/authenticated_system.rb, not @current_user instance variable.

I must say as someone new to rails, whether to use :thingy, @thingy, or
“thingy” is confusing, and I’m used to that from Perl, and of course,
I’m sure I’ll figure it out over time.

Here is my partial, account/smart_login
(app/views/account/_smart_login.rhtml):

<% if not logged_in? %>
<%= start_form_tag :controller => ‘account’, :action => ‘login’ %>

Login

<%= text_field_tag ‘login’ %>


Password

<%= password_field_tag ‘password’ %>


<%= submit_tag ‘Log in’ %>


<%= end_form_tag %>
<%= link_to(“Register for an Account”, :controller => ‘account’,
:action => ‘signup’) %>
<%= link_to(“Forgot your password?”, :controller => ‘account’, :action
=> ‘forgot_password’) %>
<% else %>
<%= start_form_tag :controller => ‘account’, :action => ‘logout’ %>
Logged in as:

<%= current_user.first_name %> <%= current_user.last_name%>

<%= submit_tag ‘Log out’ %>
<%= end_form_tag %>
<% end %>

Which I then render from application app/views/layouts/application.rhtml

<%= render(:partial => "account/smart_login") %>

Thanks a lot for the suggestions and help, everyone!

Regards,
Rich

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs