rails用户登录
要使用户登录上去,要验证用户的密码与帐号匹配
在pages_controller.rb中写代码如下
def user_landing
@user = User.authenticate(params[:id], params[:pass])
if @user
session[:user_id] = @user.id
flash[:notice]="登录成功"
render 'student'
else
flash[:notice]="帐号与密码不匹配"
render 'landing'
end
end要在modles下的user.rb中写authentlcate的方法
代码
def self.authenticate(id, password)
user = User.find_by_user_id(id)
if user && password == user.password
return user
end
false
endhtml页面的代码如下
代码
<%= form_tag("/user_landing") do %>
<%= label_tag("ID:") %>
<%= text_field_tag(:id) %>
<br>
<%= label_tag("Password:") %>
<%= password_field_tag(:pass) %>
<br>
<%= submit_tag("登录") %>
<%end%>
</div>
<% if flash[:notice]%>
<div><%= flash[:notice] %></div>
<% end %>这是我在学习ruby on rails 时遇到的问题
相关推荐
EricNet 2020-07-05
EricNet 2020-05-27
何志文 2020-05-11
JOO 2020-04-26
happyfreeangel 2020-04-09
Poisedflw 2020-03-23
yangliuhbhd 2020-03-06
Ben的程序员生涯 2013-06-01
chenshuixian 2013-06-01
wes0 2014-05-31
mrice00 2019-12-20
EricNet 2019-12-11
89304896 2019-12-08
lihaoningxia 2013-07-09
userguanguan 2015-03-16