0

I have included validation for my sign up page, but when I load the sign-up page Error appears. It should show errors after clicking submit button What i need to do?

My sign-up page

 <% if !flash[:notice].blank? %> <div class="notice"> <%= flash[:notice] %> </div> <% end %> <%= form_for @user,validate: true do |f| %> <% if [email protected]? %> <% if @user.errors.any? %> <ul class="Signup_Errors"> <% for message_error in @user.errors.full_messages %> <li>* <%= message_error %></li> <% end %> </ul> <% end %> <% end %> <%= f.label("Name:") %> <%= f.text_field(:user_name, class: "form-control") %></br> <%= f.label("Email Address:") %> <%= f.text_field(:email_id, class: "form-control") %></br> <%= f.label("Password:") %> <%= f.password_field(:password, class: "form-control") %></br> <%= f.label("Confirm Password:") %> <%= f.password_field(:password_confirmation, class: "form-control") %></br> <%= f.submit("Register",class: "btn btn-primary") %> <a class="btn btn-primary" style="margin-left:20px" href="/login" >Login</a> <% end %> 

My model where I have included my validations

User.rb

class User < ApplicationRecord has_many :reviews has_secure_password EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i validates :user_name, :presence => true, :on => :create, :uniqueness => true, :length => { :in => 3..20 } validates :email_id, :presence => true, :on => :create, :uniqueness => true, :format => EMAIL_REGEX validates :password, :presence => true, :on => :create, :confirmation => true #password_confirmation attr validates :password_confirmation, :presence => true end 

Controller

 def new puts "****Inside New Method******" @user = User.new end def create puts "****Inside create Method******" @user = User.new(user_params) puts @user.user_name if @user.save puts "** USER DETAILS SAVED SUCCESSFULLY****" flash[:notice] = "Registration successful, please login" flash[:color] = "valid"; redirect_to "/login" else flash[:notice] = "Invalid Form" flash[:color] = "invalid" end end 

When I load the page, Error appears before clicking the submit button I have included :on => :create but it doesn't work

please help!!

2
  • Can you post your controller code? Are you trying to use client side validation gem? or just server side validation (normal/default) ? Commented Jan 9, 2017 at 5:26
  • I have posted controller code. Not client side validation, I am using normal validation Commented Jan 9, 2017 at 5:44

2 Answers 2

0

if [email protected]? on the form might be causing this. This is not necesarry, since you are only checking if the record is valid in the controller, just before saving your record.

You should also add render :new when the record could not be saved, otherwise you get a view missing error.

Sign up to request clarification or add additional context in comments.

4 Comments

If i remove [email protected] also its throwing error, because of empty object @user causes @user.errors.any? is true
But if you haven't submitted the form yet, there shouldn't be any errors.
Not . part of answering this question, but you should consider using Devise gem for this type of business.
Before submitting, I have initialized to @user = User.new so it would be empty so its throwing error. Using Devise is my last option, is there any other solution?
0

Open rails console and try to manually create the user. Is it create or not?

eg:

user = User.new(username: "test", email_id: "[email protected]", password: ...) 

and check the validation by using

user.valid! 

you will get the error message if validation fails. If no error then save it

user.save! 

2 Comments

My Concern is error message is displaying when the page loads, actually error should be displayed while clicking on submit button
so are you able to create record manually on rails console? Have you checked it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.