0

在我的网站上,我正在检查用户是否已通过身份验证。我的控制器中有以下内容..

def index
  login_required
  @profile = current_user.profile  ### line 9
  ..

然后在我的 application_controller 中,我定义了 login_required 方法。

def login_required
  unless current_user
    redirect_to root_path, :notice => "Please login first"
  end
end

即使我的日志显示重定向到根目录正在发生,它仍然没有真正将我带到根路径,而是将我带回我的控制器->索引并在“@profile = current_user.profile”行出现错误. 这是日志..

Started GET "/meetings" for 127.0.0.1 at 2012-12-05 12:18:12 -0800
  Processing by xyzController#index as HTML
Redirected to http://localhost:3000/
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `profile' for nil:NilClass):
  app/controllers/xyz_controller.rb:9:in `index'

为什么我没有被重定向到根路径(这是我的主页),即使它说它正在重定向?有任何想法吗?谢谢。

4

1 回答 1

0

处理后继续进行redirect_to- 没有隐含的return. 创可贴修复是return根据 的结果在索引操作中添加一个login_required,但更好的方法是在控制器中使用过滤器。

于 2012-12-05T20:50:14.780 回答