我们为我们的 Ruby on Rails 项目实现了一个简单的登录方法,我们在页面上有一个按钮来提交用户将输入的登录信息,但是提交按钮不是指向我们的登录方法,而是运行我们的创建方法并尝试创建另一个用户。
def login
username = params[:username]
password = params[:password]
if(username == NIL || password == NIL)
loginfailed
else
comparisonUser = User.where("username = ?", username)
if(comparisonUser == NIL)
loginfailed
else
comparisonPassword = comparisonUser.password
if(comparisonPassword == password)
flash[:notice] = "Login Successful"
@user = comparisonUser
else
loginfailed
end
end
end
end
这是创建方法:
def create
@user = User.create!(params[:user])
flash[:notice] = "User, #{@user.username} was successfully created."
redirect_to questions_path
end
以及网页上登录按钮的代码:
= form_tag users_path do
= label :userlogin, :username, 'Username'
= text_field :userlogin, :username
%br/
= label :userlogin, :password, 'Password'
= text_field :userlogin, :password
%br/
= submit_tag 'Login', :id => 'Login'
我可能需要向 submit_tag 添加更多信息,但我不确定该怎么做。
谢谢您的帮助!