我正在构建我的第一个模块化应用程序,我的第一个模块是Account,我的目标是让这个模块通过Devise处理所有各种用户模型授权。
我的第一个用户类型是“用户”,这将是所有普通员工登录到应用程序的地方“(我决定采用模块化,因为我有一些应用程序即将出现,需要类似的需求和模块化的东西我可以插上看起来很诱人。
我的问题:
我创建了用户模型,但是添加了自定义注册控制器,如下所示。
user_registrations_controller.rb
module Account
class UserRegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:account_users).permit(:email, :password, :password_confirmation)
end
def account_update_params
params.require(:account_users).permit(:email, :password, :password_confirmations, :current_password)
end
end
end
用户模型如下
module Account
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
end
end
当我尝试注册用户时,我在rails服务器上的输出是:
Started POST "/account/users" for ::1 at 2016-04-25 00:49:12 -0600
Processing by Account::UserRegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"73uo7aB0DmS/rsZRuopEFvQ3MoTv1YJjdIRishvqEdY7Y2vcrIzwp6gfmUM3fApcDz2Fer5kFA/SjhNibZOGyw==", "user"=>{"email"=>"swilson@taurenltd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)
最后这里是一个屏幕抓取更好的错误宝石。
以及我的佣金路线输出概述
Prefix Verb URI Pattern Controller#Action
account /account Account::Engine
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) account/user_registrations#cancel
user_registration POST /users(.:format) account/user_registrations#create
new_user_registration GET /users/sign_up(.:format) account/user_registrations#new
edit_user_registration GET /users/edit(.:format) account/user_registrations#edit
PATCH /users(.:format) account/user_registrations#update
PUT /users(.:format) account/user_registrations#update
DELETE /users(.:format) account/user_registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user_unlock POST /users/unlock(.:format) devise/unlocks#create
new_user_unlock GET /users/unlock/new(.:format) devise/unlocks#new
GET /users/unlock(.:format) devise/unlocks#show
Routes for Account::Engine:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) account/user_registrations#cancel
user_registration POST /users(.:format) account/user_registrations#create
new_user_registration GET /users/sign_up(.:format) account/user_registrations#new
edit_user_registration GET /users/edit(.:format) account/user_registrations#edit
PATCH /users(.:format) account/user_registrations#update
PUT /users(.:format) account/user_registrations#update
DELETE /users(.:format) account/user_registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user_unlock POST /users/unlock(.:format) devise/unlocks#create
new_user_unlock GET /users/unlock/new(.:format) devise/unlocks#new
GET /users/unlock(.:format) devise/unlocks#show
我在这里非常慌张..这类事情上没有很多文件。在此先感谢您的帮助!
编辑#1 - 添加路由.rb(引擎和应用之间相同)
mount Account::Engine => "/account", as: 'account'
devise_for :users,{
class_name: "Account::User",
module: :devise,
controllers: { registrations: 'account/user_registrations' }
}
添加注册新表单
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</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 "users/shared/links" %>
修改注册视图/表单
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><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="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
答案 0 :(得分:1)
查看发送给控制器的参数:
{"utf8"=>"✓", "authenticity_token"=>"73uo7aB0DmS/rsZRuopEFvQ3MoTv1YJjdIRishvqEdY7Y2vcrIzwp6gfmUM3fApcDz2Fer5kFA/SjhNibZOGyw==", "user"=>{"email"=>"swilson@taurenltd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
表单将user
发送为params,因为在设计视图中定义了resource
和resource_name
。
因此相关的哈希键是:user
而不是:account_users
。以下更改应解决此问题:
params.require(:user).permit(...)
或者,您可以覆盖form_for
:
form_for resource, as: 'account_user' do |f|
....
如果你走这条路,你可能想要使用account_user
代替account_users
,因为你正在创建/更新单一资源,而不是集合。