AuthLogic setup issues

I am 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 mistake, but I've been pushing a brick wall on 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've got Faker generating some data for my users table, but I can't login! Every time I try it just redirects to the index. Where am I going wrong? Thanks everyone.

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

I have implemented Jakub Hampl's suggestion with form_for now - I am getting 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 haven't changed the controller at all. Thanks to everyone who answers this thread - it's incredibly helpful to me. What can I do to overcome this obstacle?

------ 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 should be changed to @user_session?

+2


a source to share


2 answers


It is a good idea to use form_for

, if possible:

<% 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 %>

      



It is also difficult to say. Does your log file provide any details (like errors)? Also try adding errors to the table to you so you can see what's going wrong.

Since it seems that the latest update is not installed @user_session, just run it and created <% form_for UserSession.new do |f| %>

.

+1


a source


in your case, params [: user_session] is empty because it is not set in your view. I think Jakub Hampl's suggestion for using form_for is the best way, but you can also stay with form_tag by giving the input names user_session[login]

and user_session[password]

, or you can change the string in your action to @user_session = UserSession.new(:login => params[:login], :password => params[:password])

.



+1


a source







All Articles