Rails 7 Signup Form Doesn't Show Error Messages

Rails 7 signup form doesn't show error messages

If you look at the logs you can see that Rails is getting an AJAX request in the form of a turbo stream:

Processing by UsersController#create as TURBO_STREAM

Where it should read:

Processing by UsersController#create as HTML

To disable turbo you want need to set a data-turbo="false" attribute on the form:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: @user, data: { turbo: false }) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>

The local: false option only works with the old Rails UJS javascript library which was the default prior to Rails 7. You can also disable Turbo by default with:

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

See:

https://turbo.hotwired.dev/handbook/drive#disabling-turbo-drive-on-specific-links-or-forms

Rails tutorial chapter 7 sign up form error doesn't appear right

Ok I fixed it by deleting everything from the scaffolds.css.scss. Not sure what this file is or how code got in here though.

Rails error messages break signup form

Why are you using Bootstrap 3? The tutorial tells you to use

gem 'bootstrap-sass', '2.3.2.0'

in your gemfile. You really should do so, because the tutorial provides you with a whole lot of code that is meant to be used with Bootstrap 2. Of course, it breaks with Bootstrap 3. If you really want to use B3 you will have to change quite a few class names in your views. Among other changes do this:

  • change div class="alert alert-error" to div class="alert alert-danger"
  • apply form-group and form-control classes to your form fields (see example here).
  • in your CSS:

    .field_with_errors {
    @extend .has-error;
    }
  • Then do all the other changes mentioned here.

Rails not showing validation error messages

Assuming you've named the User in question as @user in the controller change the form to

<% form_for :user, @user, :url => { :action => "signup" } do |f| %>

This might work as well

<% form_for @user, :url => { :action => "signup" } do |f| %>

Then in the form change accordingly:

<%= f.label(:first, "First Name")%>
<%= f.text_field :first %>

So add f. and remove _tag on each one.

And the errormessage part to:

<%= f.error_messages :header => "Please complete all the fields", :message => "There is a problem with one or more fields" %>

Not sure about how the :header and :message work in this last one, but I hope you get the idea.

UPDATEd the form tags, because they had a bug.

Simple Form not showing errors at top

With some help from Michael Koper, we were able to sort this out. The controller methods were missing status: :unprocessable_entity on the format.html statements. So changing:

format.html { render action: "new"}

to

format.html { render action: "new", status: :unprocessable_entity }

Solved this issue.

how to display devise error messages when i'm putting the login in and sign up forms in same page

I found the solution to this problem on StackOverFlow some time ago. Here's what worked for me

# In application.html.erb
<% flash.each do |name, msg| %>

# New code (allow for flash elements to be arrays)
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :div, message, :id => "flash_#{name}" %>
<% end %>
<% else %>

# old code
<%= content_tag :div, msg, :id => "flash_#{name}" %>

<% end %> #don't forget the extra end
<% end %>

and

# Wherever you want Devise's error messages to be handled like 
# your other error messages
# (in my case, registrations_controller.rb, a custom controller)
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages

See original post here

...and think about accepting answer, 50% is a bit low! ;)

===== EDIT =====

If you need to redirect to another page when errors occurs, you'll have to override controllers (check Devise Wiki or search stackoverflow for howto) but it should look like something like that

# CUSTOM DEVISE CONTROLLER
class RegistrationsController < Devise::RegistrationsController

# POST /resource
def create
build_resource

if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
# Solution for displaying Devise errors on the homepage found on:
# https://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
redirect_to root_path # HERE IS THE PATH YOU WANT TO CHANGE
end
end
end


Related Topics



Leave a reply



Submit