`
任清宇
  • 浏览: 20607 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

数据验证

    博客分类:
  • ruby
 
阅读更多

最近在做一个登录系统,在已知数据库中 存在的账号和密码,我们如何通过验证账号和密码匹配 而登陆到对应的界面呢?

 <%= form_tag("/user_landing") do %>
            <h3> <%=label_tag("账号:") %>
              <%=text_field_tag(:id) %></h3>

            <h3> <%=label_tag("密码:") %>
              <%= password_field_tag(:pass) %></h3>
            <% if flash[:notice] %>

                <div><%= flash[:notice] %></div>
            <% end %>
            </div>
            <div id="land">
              <h3>
                <%=submit_tag("登录") %>
        <% end %></h3>
            </div>

 通过上面代码 可以在点击登录的同时 进入数据库查看,密码是否和账号相匹配,若匹配可登录,

 def user_landing
    if params[:id]=='ad'&&params[:pass]=='ad'
      render 'manager'
    else
      @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

 这一段是验证 账号和密码是一致的才可以登录,不一致会出现提示,账号与密码不匹配. 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics