Home > Enterprise >  Ruby on Rails form_with not showing errors, stuck on server side
Ruby on Rails form_with not showing errors, stuck on server side

Time:01-27

I'm sure I'm just missing something really simple but I'm pretty stumped. I have a user signup form that's properly identifying errors in the signup process on the server, but it's not passing that information back to the views to render the errors above the form.

controllers/registrations_controller.rb

class RegistrationsController < ApplicationController
    def new
        @user = User.new
    end

    def create
        @user = User.new(user_params)
        if @user.save
            redirect_to root_path, notice: "Successfully signed up!"
        else
            puts 'ERRORS BELOW:'
            puts @user.errors.full_messages
            render :new
        end
    end

    private

    def user_params
        params.require(:user).permit(:email, :password, :password_confirmation)
    end
end

views/registrations/new.html.erb

<%= form_with model: @user, url: signup_path do |f| %>
    <% if @user.errors.any? %>
        <div >
            <% @user.errors.full_messages.each do |message| %>
                <div><%= message %></div>
            <% end %>
        </div>
    <% end %>

    <div class='mb-3'>
        <%= f.label :email %>
        <%= f.text_field :email, class:'form-control', placeholder: '[email protected]' %>
    </div>
    <div class='mb-3'>
        <%= f.label :password %>
        <%= f.password_field :password, class:'form-control', placeholder: 'password' %>
    </div>
    <div class='mb-3'>
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, class:'form-control', placeholder: 'password' %>
    </div>

    <div >
        <%= f.submit 'Get Started', class: 'btn btn-primary' %>
    </div>
<% end %>

routes.rb

Rails.application.routes.draw do
    root to:'main#index'

    get "signup", to: 'registrations#new'
    post "signup", to: 'registrations#create'

    get "about-us", to: 'about#index', as: :about
end

View from my console showing the errors getting printed out serverside

Started POST "/signup" for 127.0.0.1 at 2023-01-26 10:18:51 -0600
Processing by RegistrationsController#create as TURBO_STREAM
    Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"asdfasdg", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Get Started"}
ERRORS BELOW:
Password confirmation doesn't match Password
Email Must be a valid email address
    Rendering layout layouts/application.html.erb
    Rendering registrations/new.html.erb within layouts/application
    Rendered registrations/new.html.erb within layouts/application (Duration: 3.8ms | Allocations: 2734)
    Rendered shared/_navbar.html.erb (Duration: 0.2ms | Allocations: 134)
    Rendered shared/_flash.html.erb (Duration: 0.1ms | Allocations: 33)
    Rendered layout layouts/application.html.erb (Duration: 54.5ms | Allocations: 17541)
Completed 200 OK in 423ms (Views: 56.7ms | ActiveRecord: 1.1ms | Allocations: 22605)

I've tried passing them in as a flash notice, as well as not even rendering :new after @user.save fails, not sure what else to try. A lot of solutions to similar problems I've found on here seem to have been solved by putting the if/else block in the create function, but I already have that in. Not sure what I'm missing.

I should also add that I am able to create a User when I enter in a proper email and matching passwords, and it does not create a user when I fail those two checks, it just doesn't pass those errors back to the client

CodePudding user response:

Processing by RegistrationsController#create as TURBO_STREAM

Probably that's the problem, but you can disable turbo

Just add data-turbo="false" attribute to this form

<%= form_with model: @user, url: signup_path, data: { turbo: false } do |f| %>

In this case it will be usual HTTP request

  • Related