When I click on sign in link it should check for valid username and password and display the message on sign in partial page, which I am calling in colorbox as a popup, but I am not able to display error messages.
This is my loginController:
def login
if request.post?
@user = User.new(params[:user])
user=User.authenticate(params[:username], params[:password])
if user.nil?
flash[:error] = 'Login unsuccessful'
redirect_to :back
else
flash[:notice] = 'Login successful'
session[:user] = user.id
redirect_to :controller=>'users', :action => 'show',:username=>user.username
end
end
end
User.rb:
def self.authenticate(username, pass)
user = find(:first, :conditions=>["username = ?", username])
return nil if user.nil?
return user if User.hash_password(pass) == user.password && username == user.username
nil
end
In my view page_sign_in.html.erb page
<%= form_for @user, :url=>{:controller=>"logins",:action=>"login"},:remote=>true,:html=>{ :onSubmit => "return checkSignupValidation()",:id=>"signin_form", :multipart=>true} do |f| %>
@user is giving error: undefined methodmodel_name’ for Nilclass`
Your problem is that there is no model for the login – there is none needed. You do not want to use
form_for, use insteadform_tag:Then I would recommend some improvments to your controller:
You do not need the @user variable, so just avoid a unneccessary database query. I do not know if it is so good to use :back, I always try to use a fixed path.