Ask A Question

Notifications

You’re not receiving notifications from this thread.

Form not displaying errors

Christopher Mutinda asked in Rails

Here is my user.rb file

class User < ApplicationRecord
has_secure_password
validates :email, presence: true, format: { with: /\A[\w.+-]+@\w+.\w+\z/, message: "must be a valid email address" }
end

Here is my registration controller.rb

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

def create
    @user = User.new(user_params)
    if @user.save
        redirect_to root_path, notice: "Successfully created account"
    else

        render :new
    end
end


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

end

Finally here is my form

Sign Up

<%= form_with model: @user, url: sign_up_path, local: true do |form| %>
<% if @user.errors.any? %>


<% @user.errors.full_messages.each do |message| %>
<%= message %>

<% end %>

<% end %>

<%= form.label :email %>
<%= form.text_field :email, class: "form-control", placeholder: "chris@apple.com" %>


<%= form.label :password %>
<%= form.password_field :password, class: "form-control", placeholder: "password" %>


<%= form.label :password_confirmation %>
<%= form.password_field :password_confirmation, class: "form-control", placeholder: "password" %>


<%= form.submit %>

<% end %>

Reply

Change
render :new

To
render :new, status: :unprocessable_entity

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,940+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.