簡體   English   中英

Rails&Devise:當用戶注冊時,如何觸發其他事件?

[英]Rails & Devise: How can I trigger other events when user registers?

例如,訪問者將創建一個我希望能夠觸發的帳戶,並使用這些參數創建其他內容,直到他們確認或登錄為止。

這是我最接近的解決方案,如果效率不高,請提出任何建議

application_controller.rb

class ApplicationController < ActionController::Base
   #other stuff
   after_filter :trigger_me, if: :devise_controller?
     def trigger_me
       #some code to execute
       #Some way to get User info just created.
     end
end

我認為更好的方法是在你的用戶模型中通過after_create 回調或其他選項,如果你需要訪問params hash中的所有內容將覆蓋Devise RegistrationsController並添加一個回調,就像討論的那樣這里: 如何在注冊Rails3和Devise后添加回調

您可以在ApplicationController中覆蓋方法#after_sign_in_path_for

http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers#after_sign_in_path_for-instance_method

  def after_sign_in_path_for(resource)
    stored_location_for(resource) ||
      if resource.is_a?(User) && resource.country.nil?
        edit_user_path resource
      else
        super
      end
  end

然后根據用戶(資源)值重定向或執行其他操作。 用戶注冊后,他們已“登錄”,但未確認。

https://github.com/plataformatec/devise#controller-filters-and-helpers

登錄用戶,確認帳戶或更新密碼后,Devise將查找重定向到的作用域根路徑。 例如,當使用:user資源時,如果user_root_path存在,則將使用user_root_path; 否則,將使用默認的root_path。 這意味着您需要在路徑中設置根:

root to: 'home#index'

您還可以覆蓋after_sign_in_path_for和after_sign_out_path_for以自定義重定向掛鈎。

您還可以定義user_root_path並在其指向的方法中執行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM