2011-06-08 86 views
14

我一直在试图找出这2天。我通过电子邮件确认确认用户帐户(通过Devise)。我终于完成了所有这些工作,但重要的是要验证一个人拥有他们声称拥有的电子邮件。因此,每当用户更改电子邮件时,我都需要再次确认。设计 - 用户编辑电子邮件后确认

为了做到这一点,我创建了registrations_controller并覆盖了update方法。主要基于Devise的设计,但我检查是否需要根据更新发送确认。

# registrations_controller.rb 
def update 
    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key) 

    send_confirmation = false 
    if params[:user][:email] != resource.email 
    send_confirmation = true 
    end 

    if resource.update_with_password(params[resource_name]) 
    set_flash_message :notice, :updated if is_navigational_format? 
    sign_in resource_name, resource, :bypass => true 

    if send_confirmation 
     resource.update_attributes(:confirmed_at => nil, :confirmation_sent_at => nil) 
     resource.send_confirmation_instructions   
    end 

    respond_with resource, :location => after_update_path_for(resource) 
    else 
    clean_up_passwords(resource) 
    respond_with_navigational(resource){ render_with_scope :edit } 
    end 
end 

我的问题是我不知道在哪里能够改变它被重定向到哪里。我有一个页面,解释“已发送电子邮件以确认您的电子邮件”。但如果我试图在用户点击“更新帐户”后登录send_confirmation_instructions之后登录(推送到登录屏幕),然后当他们通过电子邮件确认帐户时,他们被引导到我想要的页面给他们看。

我有一个自定义的守望者策略与它的一些看跌期权,我也写了图谋在放之前过滤:

# registrations_controller.rb 
def authenticate_scope! 
    puts "RegistrationsController :: authenticate_scope!" 
    puts "action : #{params[:action]}" 

    super 
end 

所以看起来它试图对用户进行认证。日志内容如下:

... 
Redirected to http://localhost:3000/users/edit 
Completed 302 Found in 3537ms 
RegistrationsController :: authenticate_scope! 
action : edit 


Started GET "https://stackoverflow.com/users/edit" for 127.0.0.1 at 2011-06-08 11:42:09 -0500 
    Processing by RegistrationsController#edit as HTML 
    User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1 
Completed in 83ms 
Warden::Strategies authenticate! 
Warden::Strategies params: {"action"=>"new", "controller"=>"sessions"} 


Started GET "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2011-06-08 11:42:10 -0500 
    Processing by SessionsController#new as HTML 
... 

那么,我该如何控制它被重定向到哪里?我是否正确重置了“确认”属性?

回答

7

我们有一个类似的问题(主要是因为确认用户是不是真的在我们的系统经批准的用户) - 并决定去同一个user_status属性。它有两个状态 - 已确认但尚未批准的“未决”,以及“已批准”。如果由于某种原因用户不再被批准(就您的情况而言,他们更改了他们的电子邮件地址),然后我们将其更改回待处理状态。

我们在applicationController上有一个before_filter来根据它们的状态来验证它们应该去的位置。

def check_user_status 
if current_user #logged in 
case current_user.status 
    when "pending" 
    redirect_to root_path #user hasn't been approved yet 
    when "approved" 
    #tracking logic here 
    end 
end 
end 

希望这会有所帮助。

5

包更新设计。这已在当前发行版本(2.0)