I’m doing a simple demo on a windows box, showing how simple it is to
do a user login page, using code from the Agile Programming with Rails
book.
Unfortunately, it’s not acting simple.
This is the view for the login.
<% @page_title = ‘Login’ %>
<%= error_messages_for ‘user’ %>
Login
<% if session[:user_id] %>
<%= start_form_tag :action => ‘login’ %>
<%= render :partial => ‘users/form’ %>
<%= submit_tag “Login” %>
<%= end_form_tag %>
<% else %>
You are logged-in.
<% end %>
<% if session[:user_id] %>
You can logout here.
<%= link_to 'Logout', :action => 'logout' %>
<% end %>
Even though both of these sections depend on the :user_id symbol in the
session hash, the first one is working and the second is not.
Also, while logout will run when told, for some reason it stays
logged-in.
Here are the login and logout actions.
def login
if request.get?
session[:user_id] = nil
@user = User.new
else
@user = User.new(params[:user])
begin
logged_in_user = @user.try_to_login
rescue
flash[:notice] = “System error in login attempt!”
render
end
if logged_in_user
session[:user_id] = logged_in_user.id
flash[:notice] = “Logged in as #{logged_in_user.name}!”
redirect_to(:controller => ‘login’, :action => ‘index’)
else
flash[:notice] = “Invalid user/password combination”
end
end
end
def logout
session[:user_id] = nil
flash[:notice] = “You are logged-out”
redirect_to(:action => ‘index’)
end
I’m lost. I’m doing something similar on a linux box and it works fine.
Is this a problem with Rails on Windows?
Thanks,
Mike
#First change the view to be
<%= error_messages_for ‘user’ %>
<%= @page_title || 'Login'
<% if @logged_in? -%>
<%= start_form_tag :action => ‘login’ %>
<%= render :partial => ‘users/form’ %>
<%= submit_tag “Login” %>
<%= end_form_tag %>
<% else -%>
You are logged-in.
<% end -%>
<% if @logged_in? -%>
You can logout here.
<%= link_to 'Logout', :action => 'logout' %>
<% end -%>
#Then add this to your calling action
@page_title = ‘Login’
@logged_in = (session[:user_id] == nil)
#and no as bad as windows is it is not causing this error
#in future try commenting out and dumping your if conditions
Keynan P. wrote:
#First change the view to be
<%= error_messages_for ‘user’ %>
<%= @page_title || 'Login' %>
<% if @logged_in? -%>
What provides @logged_in? ? That looks like a method call but you’re
referencing it like an instance variable, and it’s nowhere in my code.
I do see that my logic was bad here, but worse, in my controller, if
you visit the login page you are logged-out. This was the core issue.
Thanks,
Mike
#First change the view to be
<%= error_messages_for ‘user’ %>
<%= @page_title || 'Login' %>
<% if @logged_in? -%>
<%= start_form_tag :action => ‘login’ %>
<%= render :partial => ‘users/form’ %>
<%= submit_tag “Login” %>
<%= end_form_tag %>
<% else -%>
You are logged-in.
<% end -%>
<% if @logged_in? -%>
You can logout here.
<%= link_to 'Logout', :action => 'logout' %>
<% end -%>
#Then add this to your calling action
@page_title = ‘Login’
@logged_in = (session[:user_id] == nil)
#and no as bad as windows is it is not causing this error
#in future try commenting out and dumping your if conditions