首页 > 解决方案 > 设计和 Ruby on Rails:注册时所有验证都失败,并显示“不能为空白”

问题描述

我正在尝试注册Affiliate使用设计。

/affiliates/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

/affiliates/registrations/new.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)

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

标签: ruby-on-railsdevise

解决方案


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

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


推荐阅读