我正在使用Rails 4和Devise 3.2.3.
在我的应用程序中,我正在尝试使用已登录的管理员用户,该用户只能使用电子邮件创建新用户.
我按照这里的说明操作:
https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up
意见>确认> show.html.haml
%h2 You're almost done! Now create a password to securely access your account.
= semantic_form_for(resource, :as => resource_name, :url => confirm_path) do |form|
= devise_error_messages!
= form.inputs do
= form.input :password, :input_html => {:autofocus => true}
= form.input :password_confirmation
= form.input :confirmation_token, :as => :hidden
= form.actions do
= form.action :submit, :label => 'Confirm Account
confirmations_controller
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token]) if params[:confirmation_token].present?
super if resource.nil? or resource.confirmed?
end
def confirm
self.resource = resource_class.find_by_confirmation_token(params[resource_name][:confirmation_token]) if params[resource_name][:confirmation_token].present?
if resource.update_attributes(params[resource_name].except(:confirmation_token).permit(:email, :password, :password_confirmation)) && resource.password_match?
self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
set_flash_message :notice, :confirmed
sign_in_and_redirect(resource_name, resource)
else
render :action => "show"
end
end
end
但在我的确认节目动作中,我收到此错误:
First argument in form cannot contain nil or be empty:
= semantic_form_for(resource, :as => resource_name, :url => confirm_path) do |form|
UPDATE
问题似乎与ConfirmationsController有关,show动作似乎不起作用:
资源方法似乎不起作用.试着输出它
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token]) if params[:confirmation_token].present?
# super if resource.nil? or resource.confirmed?
render text: self.resource.nil?
end
end
返回true
我通过“send_confirmation_instructions”收到的confirmation_token也不属于数据库中的任何用户实例.
最佳答案 我找到了解决方案,
at the Devise Wiki的文档不正确.
这是Rails 4的正确代码:
class ConfirmationsController < Devise::ConfirmationsController
def show
@original_token = params[:confirmation_token]
digested_token = Devise.token_generator.digest(self, :confirmation_token,params[:confirmation_token])
self.resource = resource_class.find_by_confirmation_token(digested_token) if params[:confirmation_token].present?
super if resource.nil? or resource.confirmed?
end
def confirm
digested_token = Devise.token_generator.digest(self, :confirmation_token, params[resource_name][:confirmation_token])
self.resource = resource_class.find_by_confirmation_token(digested_token) if params[resource_name][:confirmation_token].present?
if resource.update_attributes(params[resource_name].except(:confirmation_token).permit(:email, :password, :password_confirmation)) && resource.password_match?
self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
set_flash_message :notice, :confirmed
sign_in_and_redirect(resource_name, resource)
else
render :action => "show"
end
end
end
这是正确的观点>确认> show.html.haml
%h2 You're almost done! Now create a password to securely access your account.
= semantic_form_for(resource, :as => resource_name, :url => confirm_path, :method => :patch) do |form|
= devise_error_messages!
= form.inputs do
= form.input :password, :input_html => {:autofocus => true}
= form.input :password_confirmation
= form.input :confirmation_token, :as => :hidden, :value => @original_token
= form.actions do
= form.action :submit, :label => 'Confirm Account'