我如何显示 devise sign_up 页面错误消息?

How do i display devise sign_up page error messages?

我有一个问题,每当我尝试 sign_up 并将所有输入字段留空时,我不会收到任何错误,例如“密码不能为空”。 但是在刷新页面并重新提交相同的数据后,我得到了页面上显示的错误..简而言之,为了显示 sign_up 错误,我必须刷新 sign_up 页面几次..如何我可以解决这个问题吗? ..

Notice : i make sure that i have this line: <%= render "devise/shared/error_messages", resource: resource %> , in "views/devise/registrations/new.html.erb" file.

设计共享错误部分包含以下代码:

<% if resource.errors.any? %>

<%= I18n.t("errors.messages.not_saved",
count: resource.errors.count,
resource: resource.class.model_name.human.downcase)
%>
    <% resource.errors.full_messages.each do |message| %>
    <%= message %>
    <% end %>

<% end %> 

提交后的表单没有向我显示错误消息,但在刷新页面几次后,它会将我重定向到 (/users),并向我显示错误消息...所以我有刷新页面几次以查看错误消息..

我的解决方案:

我刚刚更改了一行设计注册创建操作。

这是设计注册控制器的创建方法:

  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end

我把最后一行respond_with resource改成了render :new, status: 422

这是我修改后的创建操作。

  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      render :new, status: 422
    end
  end