[英]Rails 4: how to access the content of Devise Registrations Controller
我有一個使用Devise進行身份驗證的Rails 4應用程序-目前正在運行。
我現在正在關注有關為Rails創建作用域邀請系統的 coderwall教程。
在“ 新邀請的用戶注冊”部分中,作者建議修改RegistrationsController
,如下所示:
def new
@token = params[:invite_token] #<-- pulls the value from the url query string
end
def create
@newUser = build_user(user_params)
@newUser.save
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).user_group #find the user group attached to the invite
@newUser.user_groups.push(org) #add this user to the new user group as a member
else
# do normal registration things #
end
end
在Stack Overflow以及整個網絡上都有很多問題和答案,解釋了如何覆蓋Devise RegistrationsControllers
,包括:
因此,我了解到,創建一個將從Devise::RegistrationsController
繼承的MyDevise::RegistrationController
並在要修改的動作開始時調用super
命令將保留這些動作的原始功能。
我可能會做類似的事情:
def new
super
@token = params[:invite_token] #<-- pulls the value from the url query string
end
def create
@newUser = build_user(user_params)
@newUser.save
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).user_group #find the user group attached to the invite
@newUser.user_groups.push(org) #add this user to the new user group as a member
else
super
end
end
我唯一關心的是,在不知道該控制器及其動作的原始內容的情況下,我不願意覆蓋該控制器及其動作。
-----
更新 :我知道我們可以從Devise GitHub存儲庫訪問Devise::RegistrationsController
,但是我不確定我的還是一樣。 例如,當我實施身份驗證系統時,可能已對其進行了修改。
-----
更新2 :如果我使用第一次更新中提到的Devise::RegistrationsController
的代碼,則可以為新的RegistrationsController
提出以下代碼:
def new
@token = params[:invite_token] #<-- pulls the value from the url query string
build_resource({})
set_minimum_password_length
yield resource if block_given?
respond_with self.resource
end
def create
build_resource(sign_up_params)
resource.save
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).calendar #find the calendar attached to the invite
resource.calendars.push(org) #add this user to the new calendar as a member
else
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
end
這完全有意義嗎?
-----
因此,有沒有辦法從應用程序中的某個地方提取當前Devise::RegistrationsController
的內容?
如果沒有,我正在考慮實現的代碼是否有意義?
您可以使用:
bundle open devise # bundle will open devise folder in your editor
關於您的代碼:
有一個更清潔的解決方案。 看一下這段代碼:
def new
build_resource({})
set_minimum_password_length
yield resource if block_given?
respond_with self.resource
end
這幾乎是自然的英語,只有yield resource if block_given?
才yield resource if block_given?
可能會使您感到困惑。 這行代碼執行以下操作:如果使用塊調用了new
方法,則將resource
變量傳遞給該塊並執行它。 這樣做特別是為了簡化自定義行為的添加和覆蓋。
因此,我們可以按以下方式覆蓋此代碼:
def new
# here you don't need this `resource` variable, but somebody may need it
super do |resource|
@token = params[:invite_token]
end
end
這和你寫的絕對一樣
def new
build_resource({})
set_minimum_password_length
@token = params[:invite_token]
respond_with self.resource
end
編輯2 :試試這個:
def create
super do |resource|
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).calendar #find the calendar attached to the invite
resource.calendars.push(org) #add this user to the new calendar as a member
end
end
end
如果要查看正在使用哪個版本的Devise,請在終端機上運行以下命令: gem which devise
。 這樣您就可以根據使用的版本在其GitHub上檢查正確的源代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.