views:

156

answers:

2

Hi all,

I'm trying to set up a simple login using AuthLogic into my User table. Every time I try, the login fails and I don't know why. I'm sure this is a simple error but I've been hitting a brick wall with it for a while.

#user_sessions_controller
def create
@user_session = UserSession.new(params[:user_session])
  if @user_session.save
    flash[:notice] = "Login successful!"
  else
    flash[:notice] = "We couldn't log you in. Please try again!"
    redirect_to :controller => "index", :action => "index"
  end
end

#_user_login.html.erb (this is the partial from my index page where Users log in)
<% form_tag user_session_path do %>
  <p><label for="login">Login:</label> 
<%= text_field_tag "login", nil, :class => "inputBox", :id => "login",
  </p>

  <p><label for="password">Password: </label>
<%= password_field_tag "password", nil, :class => "inputBox", :id => "password",
  </p>

  <p><%= submit_tag "submit", :class => "submit" %></p>
<% end %>

I had Faker generate some data for my user table but I cannot log in! Every time I try it just redirects to index. Where am I going wrong? Thanks everybody.

------UPDATE------

I implemented Jakub Hampl's suggestion with form_for just now - I'm getting a new error.

ActionView::TemplateError (called id for nil, which would mistakenly be 4 -
1: <% form_for @user_session do |f| %>
2: <% if flash[:notice] -%>
3: <p class="notice"><%= flash[:notice] %></p>
4: <% end -%>

app/views/index/_user_login.html.erb:1
app/views/layouts/index.html.erb:65
app/controllers/index_controller.rb:3:in `index'

Rendered rescues/_trace (86.0ms)
Rendered rescues/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)

I have not changed the controller at all. Thank you everyone who is responding to this topic - it's incredibly helpful to me. What can I do now to get past this hurdle?

------UPDATE #2------

Here is my application controller.

def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.user
end

def require_user
  unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_path
return false
  end
end

def require_no_user
  if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to root_url
return false
  end
end

Which one of these should be changed to @user_session?

+1  A: 

A good idea is to use form_for if you possibly can:

<% form_for @user_session do |f| %>
  <p>
    <%= f.label :username %>
    <%= f.text_field :username, :class => :inputBox %>
  </p>
  <p>
    <%= f.label :password %>
    <%= f.password_field :password, :class => :inputBox %>
  </p>
  <p><%= f.submit "submit", :class => :submit %></p>
<% end %>

Apart from that hard to say. Does your log file give any detail (aka errors)? Also try to add the errors on the table to you flash so that you can see what's going wrong.

Since it seems fro your last update that @user_session is not set, just go ahead and create one: <% form_for UserSession.new do |f| %>.

Jakub Hampl
'login' works fine as well with authlogic.
x1a4
My bad, thanks for the correction.
Jakub Hampl
Edited to suite the edit of the OP.
Jakub Hampl
+1  A: 

in your case, params[:user_session] is empty because it's not being set in your view. I think Jakub Hampl's suggestion to use form_for is the best way, but you can also stay with form_tag by setting input names to user_session[login] and user_session[password], OR you can change the line in your action to @user_session = UserSession.new(:login => params[:login], :password => params[:password]) .

x1a4
This is correct. You can confirm this by inspecting the logs for that POST request. It will have the correct field submitted, but they aren't scoped to user_session like they are expected to in the controller.
Ben Scheirman