Wicked 的多步验证:验证失败:街道不能为空

Multistep Validations with Wicked: Validation failed: Street can't be blank

我有一个多步骤表单。这有点复杂,因为基本上首先用户是通过注册控制器创建的,然后被重定向到一个表单。准确的说,不是多步表单,更多的是两步注册。对这些进行验证非常棘手,但我想出了可行的方法。在我的 User.rb 中,我将验证定义如下:

validates :first_name, presence: true, :on => :create
validates :last_name, presence: true, :on => :create
validates :street, presence: true, :on => :update

但现在我遇到了问题,在更新步骤中向用户显示错误。我的 UserStepsController 中有更新操作:

class UserStepsController < ApplicationController
    include Wicked::Wizard
    steps :address
    #respond_to :html, :js

    def show
      @user = current_user || User.from_omniauth(request.env["omniauth.auth"])
      render_wizard
    end


    def update
      @user = current_user || User.from_omniauth(request.env["omniauth.auth"])
        if @user.update!(user_params)
            render_wizard @user
        else
            render :show
        end
    end


    private

    def user_params
        params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :street, :house_number, :city, :zip_code)
    end

    def redirect_to_finish_wizard(options = nil, params = nil)
        redirect_to new_user_profile_path(current_user)
    end

end

因此,如果无法更新用户,导致验证失败,我很乐意显示有关出错原因的错误消息。为此,在我的 address.html.erb 中,我定义了这种行为:

<%= form_for @user, url: wizard_path do |f| %>
    <% if @user.errors.any? %>
        <div class="error_explanation">
            <h2><%= pluralize(@user.errors.count, "error") %> prevented this record from being saved:</h2>
                <ul>
                    <% @user.errors.full_messages.each do |message| %>
                        <li><%= message %></li>
                    <% end %>
                </ul>
        </div>
    <% end %>

        <div class="row second 1">
            <div class="column">
              <div class="field wizard">
                <%= f.label :street %><br />
                <%= f.text_field :street, class: 'form-control' %>
              </div>
            </div>

这不会显示错误,而是抛出活动记录错误:

ActiveRecord::RecordInvalid in UserStepsController#update
Validation failed: Street can't be blank

提取的源代码(大约第 14 行):

12 def update
13 @user = current_user || User.from_omniauth(request.env["omniauth.auth"])
14 if @user.update!(user_params)
15 render_wizard @user
16 else
17 render :show

我做错了什么?

update vs update!。如果我记得 update! 在模型上运行 save! 而不是 save。这意味着如果记录无效,它会运行异常。 update 将尝试调用运行验证的 save 方法

问题出在更新操作上。我改变了它如下。

def update
      @user = current_user || User.from_omniauth(request.env["omniauth.auth"])
        if @user.update_attributes(user_params)
            render_wizard @user
        else
            render :address
        end
    end

我也可以改变我的看法,以引导共享的错误消息:

<%= form_for @user, url: wizard_path do |f| %>
<%= render "devise/shared/error_messages" %>

这适用于创建和更新@user:

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |message| %>
      <li>* <%= message %></li>
    <% end %>
    </ul>
  </div>
<% end %>