Devise 和 Ruby on Rails:注册时所有验证都失败,"Can't be Blank"



我正在尝试使用Devise注册Affiliate

/附属公司/registrations_controller.rb

class Affiliates::RegistrationsController < 
Devise::RegistrationsController
include StatesHelper, ApplicationHelper
before_action :configure_sign_up_params, only: [:create]
before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
def new
@plan = AffiliatePlan.find_by(nickname: params.permit(:plan)[:plan].downcase)
super
end
# GET /resource/edit
def edit
@states = us_states
super
end
# PUT /resource
def update
@states = us_states
super
if resource.address_coordinates.length > 1 
resource.services.each{ |s| s.update_attributes( {lonlat: "POINT(#{resource.address_coordinates.join(' ')})"})}
end 
end
def update_resource(resource, params)
resource.update_without_password(params)
end
protected
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:business_name, :website, :phone, :affiliate_plan_id, contact_name: [:first_name, :last_name], address: [:street_address, :address_line2, :city, :state, :zip_code]])
end
# If you have extra params to permit, append them to the sanitizer.
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:business_name, :website, :phone, :affiliate_plan_id, contact_name: [:first_name, :last_name], address: [:street_address, :address_line2, :city, :state, :zip_code]])
end
# The path used after sign up.
def after_sign_up_path_for(resource)
affiliate_signups_path
end
end

/附属公司/注册/新.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<% if @plan %>
<%= f.hidden_field :affiliate_plan_id, value: @plan.id %>
<% else %>
<%= f.hidden_field :affiliate_plan_id, value: resource.affiliate_plan_id %>
<% end %>
<%= f.fields_for :contact_name do |n| %>
<%= n.text_field :first_name, autofocus: true, placeholder: "First Name*", class: "form-control", required: true %>
<%= n.text_field :last_name, placeholder: "Last Name*", class: "form-control", required: true  %>
<% end %>
<%= f.text_field :business_name, placeholder: "Company Name", class: "form-control" %>
<%= f.email_field :email, autocomplete: "email", placeholder: "Email Address*", class: "form-control", required: true %>
<%= f.password_field :password, autocomplete: "new-password", placeholder: "Create a Password", class: "form-control", required: true %>
<%= f.password_field :password_confirmation, autocomplete: "new-password", placeholder: "Confirm Password", class: "form-control", required: true %>

<%= f.submit "Next", class: "btn btn-primary btn-sm" %>
<% end %>

路线.rb

devise_for :affiliates, path: "partners", controllers: {
sessions: 'affiliates/sessions',
registrations: 'affiliates/registrations'
}

提交表单时,我总是收到验证错误:

7 errors must be fixed
- Email can't be blank
- Password can't be blank
- Password is too short (minimum is 9 characters)
- Password must contain at least one digit
- Password must contain at least one punctuation mark or symbol
- Password must contain at least one upper-case letter
- Affiliate plan must exist

日志显示立即回滚,但没有其他信息:

Started POST "/partners" for 127.0.0.1 at 2020-05-25 14:09:59 -0400
Processing by Affiliates::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SowYVWzTqVYdwZWjYjNc3hlGC5UITqt+bKjQuSATOpLcdVGb52x7gEi8p15MmhlZrLNLpD07fCxp5Gya8/cQMg==", "affiliate"=>{"affiliate_plan_id"=>"2", "contact_name"=>{"first_name"=>"Stephen", "last_name"=>"Tilly"}, "business_name"=>"1995", "email"=>"sarwerera@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Next"}
(0.2ms)  BEGIN
↳ app/controllers/affiliates/registrations_controller.rb:18
(0.2ms)  ROLLBACK
↳ app/controllers/affiliates/registrations_controller.rb:18
Rendering affiliates/registrations/new.html.erb within layouts/application
Rendered affiliates/registrations/new.html.erb within layouts/application (2.6ms)

我不太确定从那里去哪里,有什么想法吗?

我认为这可能会起作用。

我们假设联盟会员没有计划,因为他尚未注册。The Plan must exist!

在文件new.html.erb不建议使用逻辑,如果需要,可以在控制器中执行此操作。样品:before_action :set_plan, only: [:new]

class Affiliates::RegistrationsController < Devise::RegistrationsController
include StateHelper, ApplicationHelper
before_action :set_plan, only: [:new]
before_action :configure_permitted_parameters, only: [:create]
protected
def set_plan
@plan = AffiliatePlan.find_by(nickname: params[:plan])
end
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |user_params|
user_params.permit([
:email, :password, :password_confirmation,
:business_name, :website, :phone, :affiliate_plan_id,
contact_name: [:first_name, :last_name],
address: [:street_address, :address_line2, :city, :state, :zip_code]
])
end
end
end

另一方面,如果您将会员的额外信息与会员作为用户的信息分开,那就太好了。

例:

对于用户:

会员数据是:电子邮件,密码,password_confirmation,用户名等

有关会员的额外信息:

会员信息数据为:business_name、:网站、:p磨练等

我假设您有另一个 Devise 资源设置,这就是您创建此设置的原因。我假设它是User.在这种情况下,主要原因之一可能是 Devise 期望该模型作为资源,这就是为什么它会因缺少验证而犯错误的原因。由于Devise::RegistrationsController期待包含电子邮件和密码的params[:user]

建议:如果是这种情况,我会考虑将会员配置文件与现有资源相关联,然后从那里继续。 即会员 -> 属于:到 -> 用户。这会让你的生活更轻松。

相关内容

最新更新