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

Here why user is declared as a local variable instead of instance variable @user?

4

3 回答 3

2

可能是因为视图不需要访问user对象。它只是实例化它以查看它是否是有效用户并正确验证,然后重定向到管理页面。

于 2013-03-31T01:47:28.670 回答
0

因为您通常不需要在操作之外访问用户变量

于 2013-03-31T01:47:37.783 回答
0

答案是因为此操作不需要视图。该操作包含一个 if else 块,该块都重定向,因此实例变量没有用处。

于 2013-03-31T12:21:32.883 回答