1

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 %>
4
  • how does your shared/error_messages look? Are you saying that the error message is always displayed or its not displayed irrespective of what you do? Commented Feb 9, 2016 at 14:26
  • I edited my question. In fact, the shared secret is displaying The form contains 5 errors. all the time, and I get my 5 errors, like Name can't be blank for example. Whatever I send in the form, errors are here. Commented Feb 9, 2016 at 14:33
  • change in form form_for(:user to form_for @user, and in general this string can looks like <%= form_for @user do |f| %> because rails understands which controller to use and what action you need Commented Feb 9, 2016 at 14:40
  • Always the same problem, however I get this in the debug message : user: !ruby/hash:ActionController::Parameters name: Baptiste email: '' password: '' password_confirmation: '' so I guess that object is set Commented Feb 9, 2016 at 15:15

1 Answer 1

2

You are using strong parameters, but incorrectly: Simply do User.new(user_params)

user_params, as defined, becomes a hash of permitted params and it's value. The original params hash, has no user_params keys.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.