简体   繁体   中英

How do i display devise sign_up page error messages?

I have a problem that whenever iam trying to sign_up and leave all input fields empty i don't get any errors like "password can't be empty".. but after refreshing the page and resubmit the same data i get the errors displayed on the page.. in short in order to display sign_up errors i have to refresh the sign_up page a couple of times.. how can i solve this issue? ..

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

The devise shared error partial contains the following code:

<% 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 %> 

The form after being submitted there is no error messages displayed to me, but after refreshing the page a couple of times it redirects me to (/users), with the error messages displayed to me...so i have to refresh the page a couple of times to see the error messages..

The solution for me:

I just changed one line of devise registration create action.

This is the creat method of devise registration controller:

  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

I changed the last line respond_with resource to be render:new, status: 422

And this is the create action with the modification that I have made.

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM