authenticate_user怎么样!工作?

时间:2011-12-30 10:00:19

标签: ruby-on-rails devise

我无法登录以使用设计。

所以......这就是我在登录行动中发布的内容:

Started POST "/sign_in" for 127.0.0.1 at 2011-12-30 17:53:14 +0800
  Processing by UsersController#sign_in as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"k/z12JAUDjFaLtw6X+dL5xa7ZtcLKYmxGSc6SAvaFlE=", "user"=>{"email"=>"fivetwentysix@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "x"=>"0", "y"=>"0"}
Completed 401 Unauthorized in 0ms
  Processing by UsersController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"k/z12JAUDjFaLtw6X+dL5xa7ZtcLKYmxGSc6SAvaFlE=", "user"=>{"email"=>"fivetwentysix@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "x"=>"0", "y"=>"0"}
Rendered users/new.html.haml within layouts/application (31.7ms)
Completed 200 OK in 40ms (Views: 38.8ms | ActiveRecord: 0.9ms)

这是我尝试但不起作用的......

 def sign_in
   authenticate_user!
   if signed_in?
     redirect_to root_path
   else
     redirect_to sign_in_path
   end
 end

所以我认为我错过了对设计中所有这些“魔法”如何起作用的理解。事情似乎有点过于抽象,我觉得与设计背后的业务逻辑隔离,这让我迷失了,因为我不确定如何告诉authenticate_user!从我的参数中读取。

所以我的直觉告诉我要查找authenticate_user!

但那定义在哪里?

我在设计文档中有一个简短的介绍但是找不到那里定义的方法。

1 个答案:

答案 0 :(得分:0)

如果您想在登录/退出或编辑个人资料后使用自己的重定向(如果我已理解您的意思),我建议使用这种方法:

class ApplicationController < ActionController::Base
  private

  def after_sign_out_path_for(resource_or_scope)
    root_path
  end

  def after_sign_in_path_for(resource_or_scope)
    root_path
  end

  def after_update_path_for(resource_or_scope)
    root_path
  end
end