2013-03-31 30 views
0
def create 
    user = User.find_by_name(params[:name]) 

    if user and user.authenticate(params[:password]) 
    session[:user_id] = user.id 
    redirect_to admin_url 
    else 
    redirect_to login_url, alert: "Invalid user/password combination" 
    end 

end 

这里为什么用户被声明为局部变量而不是实例变量@user?为什么我们不在认证中使用@var?

回答

2

可能因为视图不需要访问user对象。它只是实例化它,看它是否是一个有效的用户,并且正确地进行了身份验证,然后重定向到管理页面。

0

因为你不一般需要访问的动作

0

答案是,因为没有必要为这个动作视图之外的用户变量。该动作包含一个if else块,它们都重定向,因此对实例变量没有用处。

相关问题