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
  end

 html页面的代码如下

代码

<%= 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 时遇到的问题

相关推荐