I applied Devise in two models: Guardian and User.
Taking User for instance:
Controller (users/registrations_controller.rb):
class Users::RegistrationsController < Devise::RegistrationsController
layout 'devise', only: [:new, :create]
before_action :configure_sign_up_params, only: [:create]
# GET /resource/sign_up
def new
super
end
# POST /resource
def create
if !params[:user][:birth_year].blank? && !params[:user][:birth_month].blank? && !params[:user][:birth_day].blank?
params[:user][:birthday] = Time.new(params[:user][:birth_year], params[:user][:birth_month], params[:user][:birth_day], utc_offset="+08:00")
end
@user = User.new params.require(:user).permit(:username, :nickname, :birthday, :password, :email).merge(locale: locale)
if verify_rucaptcha?(@user) && @user.save
sign_in @user
redirect_to new_guardian_registration_path
else
render :new
end
end
protected
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :username, :birth_year, :birth_month, :birth_day, :terms])
end
end
Routes (config/routes.rb):
Rails.application.routes.draw do
...
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
}, path_names: {
sign_in: 'login',
sign_out: 'logout',
sign_up: 'signup'
}
...
end
When failure to create new user, it will render :new action and to pass inputted parameters in the new form (page). However, its display url (in browser) changed from 'localhost:3000/users/signup' to 'localhost:3000/users'. It's work but if refresh page, Routing error will be generated: 'No route matches [GET] "/users"'.
I tried to change "render :new" to "redirect_to new_user_registration_path", the display url (in browser) is OK ('localhost:3000/users/signup') but the form status and inputted parameters will not passed correctly.
/users
. This is a measure to avoid the user inadvertently resending a non-safe request. - max