I try to validate a signup form and display an error if it's needed, but whatever I write in this form (or not write), there are all errors possible displayed (invalid email, blank field etc...). I followed a tutorial and I can't see where is the problem.
Here is my form :
<div class="row">
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.text_field :name, :class => "col-lg-4 col-lg-offset-4 field", :placeholder => "Name", :maxlength => "80" %></br>
<%= f.text_field :email, :class => "col-lg-4 col-lg-offset-4 field", :placeholder => "Email", :maxlength => "80" %></br>
<%= f.password_field :password, :class => "col-lg-4 col-lg-offset-4 field", :placeholder => "Password", :maxlength => "80" %></br>
<%= f.password_field :password_confirmation, :class => "col-lg-4 col-lg-offset-4 field", :placeholder => "Password confirmation", :maxlength => "80" %></br>
<%= f.submit :Submit, :value => "Sign up", :class => "col-lg-2 col-lg-offset-5", :id => "signup" %>
<button type="button" class="btn btn-link col-lg-4 col-lg-offset-4"><%=link_to "Already registered ?", home_path%></button>
<% end %>
my controller :
class UsersController < ApplicationController
def signup
@user = User.new
end
def create
@user = User.new(params[user_params])
if @user.save
end
render "signup"
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
and my model :
class User < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password
after_save :clear_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 6..20, :on => :create
def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
end
end
end
Here is my shared_secret :
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
shared/error_messageslook? Are you saying that the error message is always displayed or its not displayed irrespective of what you do?The form contains 5 errors.all the time, and I get my 5 errors, likeName can't be blankfor example. Whatever I send in the form, errors are here.form_for(:usertoform_for @user, and in general this string can looks like<%= form_for @user do |f| %>becauserailsunderstands which controller to use and what action you needuser: !ruby/hash:ActionController::Parameters name: Baptiste email: '' password: '' password_confirmation: ''so I guess that object is set