我想在Devise Signup之上 Build 注册时创建一个自定义确认方法
我遵循了this post的指导方针,这是我的注册控制器,被覆盖:
class RegistrationsController <Devise :: RegistrationsController
skip_before_filter :require_no_authentication, :only => [ :create, :edit, :update, :create_by_user ]
# before_filter :configure_permitted_parameters, if: :devise_controller?
def new
super
end
def create
build_resource(sign_up_params)
puts "what is resource_name: #{resource_name}"
resource.confirmed ||= false
resource.confirmation_code ||= create_random_pass
if resource.save
yield resource if block_given?
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)
UserMailer.send_simple_message(resource).deliver
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
respond_with resource
end
end
def edit
super
end
def update
super
end
def confirmation
@user = User.find(params[:user_id])
end
def confirm
## handles the confirmation
puts "The params: #{params[:confirmation_code]}"
puts "Hello"
user = User.find(params[:user_id])
if user.confirmed == false
puts "It's false"
redirect_to '/'
elsif user.confirmed == true
puts "It's true"
else
puts "It's nil"
end
end
我匹配的路线如:
devise_scope :user do
root to: 'static_pages#home'
match '/sessions/user', to: 'devise/sessions#create', via: :post
match '/users/newuser', to: 'registrations#create_by_user', via:[:post, :get]
match '/users/:user_id/confirmation/', to: 'registrations#confirmation', via: :get
match '/users/:user_id/confirm', to: 'registrations#confirm', via: :post
结束
当我在'/ users /:user_id / confirmation'中提交表单时,我收到一个错误:
NameError in UsersController#update
undefined local variable or method `signed_in_user' for #<UsersController:0x00000005696e00>
我显然不完全明白设计是做什么的,因为我不明白为什么他称之为用户控制器而不是注册控制器
编辑:
我的UsersController:
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
def show
@user = User.find(params[:id])
end
def new
@user = User.new
3.times { @user.accounts.build }
end
def create
# @user = params[:user] ? User.new(user_params) : User.new_guest
@user = User.new(user_params)
@account = @user.accounts.new(account_params)
if @user.save && @account.save
sign_in @user
##flash[:success] = "Welcome to the HighTide!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
#flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
## I tried also putting in here the logic of the confirmation code
def confirmation
@user = User.find(params[:user_id])
end
def confirm
puts "The params: #{params[:confirmation_code]}"
puts "Hello"
# self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
# prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
user = User.find(params[:user_id])
if user.confirmed == false
puts "It's false"
redirect_to '/'
elsif user.confirmed == true
puts "It's true"
else
puts "It's nil"
end
end
private
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def user_params
# params.require(:user).permit(:name, :email, :password,
# :password_confirmation, accounts_attributes: [:id, :title])
params.fetch(:user, {}).permit(:name, :email, :password,
:password_confirmation, accounts_attributes: [:id, :title])
end
end
1 回答
使用authenticate_user而不是signed_in_user . 这是设计配置所必需的 .
您在用户控制器中有一个之前的操作调用 . 这需要一个名为signed_in_user的方法 . 你为什么那么?这与设计无关 .