缺少参数或值为空 - Rails 4 引擎
param is missing or the value is empty - Rails 4 Engine
我正在构建我的第一个模块化应用程序,我的第一个模块是帐户,我的目标是让这个模块通过 Devise 处理所有各种用户模型授权。
我的第一个用户类型是 "User",这将是所有普通员工登录应用程序的地方”(我决定对此进行模块化,因为我有一些应用程序需要类似的需求和我可以插入的模块化东西看起来很诱人。
我的问题:
我已经创建了用户模型,但是添加了一个自定义注册控制器,如下所示。
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)
最后是从更好的错误 gem 中截取的屏幕截图。
以及我的佣金路线输出概览
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" %>
编辑注册View/Form
<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 %>
查看发送到您的控制器的参数:
{"utf8"=>"✓", "authenticity_token"=>"73uo7aB0DmS/rsZRuopEFvQ3MoTv1YJjdIRishvqEdY7Y2vcrIzwp6gfmUM3fApcDz2Fer5kFA/SjhNibZOGyw==", "user"=>{"email"=>"swilson@taurenltd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
表单将参数发送为 user
因为这就是 resource
和 resource_name
在设计视图中的定义。
所以相关的散列键是:user
而不是:account_users
。以下更改应该可以解决此问题:
params.require(:user).permit(...)
或者,您可以覆盖 form_for
:
中的参数名称
form_for resource, as: 'account_user' do |f|
....
如果你走那条路,你可能想要使用 account_user
而不是 account_users
,因为你是 creating/updating 一个单一的资源,而不是一个集合。
我正在构建我的第一个模块化应用程序,我的第一个模块是帐户,我的目标是让这个模块通过 Devise 处理所有各种用户模型授权。
我的第一个用户类型是 "User",这将是所有普通员工登录应用程序的地方”(我决定对此进行模块化,因为我有一些应用程序需要类似的需求和我可以插入的模块化东西看起来很诱人。
我的问题:
我已经创建了用户模型,但是添加了一个自定义注册控制器,如下所示。
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)
最后是从更好的错误 gem 中截取的屏幕截图。
以及我的佣金路线输出概览
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" %>
编辑注册View/Form
<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 %>
查看发送到您的控制器的参数:
{"utf8"=>"✓", "authenticity_token"=>"73uo7aB0DmS/rsZRuopEFvQ3MoTv1YJjdIRishvqEdY7Y2vcrIzwp6gfmUM3fApcDz2Fer5kFA/SjhNibZOGyw==", "user"=>{"email"=>"swilson@taurenltd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
表单将参数发送为 user
因为这就是 resource
和 resource_name
在设计视图中的定义。
所以相关的散列键是:user
而不是:account_users
。以下更改应该可以解决此问题:
params.require(:user).permit(...)
或者,您可以覆盖 form_for
:
form_for resource, as: 'account_user' do |f|
....
如果你走那条路,你可能想要使用 account_user
而不是 account_users
,因为你是 creating/updating 一个单一的资源,而不是一个集合。