1
我正在構建一個只允許管理員帳戶創建新用戶帳戶的應用程序。我使用devise 4.2進行身份驗證,cancancan可以使用rails 4.2.6進行授權。我的問題是沒有創建新用戶,並且我沒有收到任何錯誤消息,所以不知道發生了什麼問題。 我登記控制器:設計未保存的新用戶
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
before_action :configure_account_update_params, only: [:update]
#before_action :authorize_admin, only: [:create, :new, :destroy]
before_action :authenticate_user!
load_and_authorize_resource
# GET /resource/sign_up
def new
super
end
# POST /resource
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
#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}"
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
# GET /resource/edit
def edit
super
end
# PUT /resource
def update
super
end
# DELETE /resource
def destroy
super
end
def cancel
super
end
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
def after_sign_up_path_for(resource)
super(resource)
end
def after_inactive_sign_up_path_for(resource)
super(resource)
end
end
我的觀點:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email%>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
我評論指出,在帳戶創建一個新用戶的跡象,因爲我得到的提示信息說:「你已經登錄」行當以管理員身份登錄時創建新用戶時,導致我相信這就是新用戶未被保存的原因。這似乎是不正確的,雖然當我嘗試創建一個新用戶時,我仍然收到閃光消息說同樣的事情。任何幫助表示讚賞。