user1072337
user1072337

Reputation: 12945

Ruby on Rails Sign Up Page Issues (spitting out error without an error provided)

I am trying to work on a simple signup page using Ruby on Rails, however I'm running into an issue. When a user is logging in, the validation errors I have set up are being triggered when they should not be (it is saying the password is not long enough when it is).

Here is some relevant code:

user_controller.rb:

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

User.rb model:

class User < ActiveRecord::Base
    #attr_accessor :name, :email, :password_digest
    before_save { self.email = email.downcase }
    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true,
                      format: { with: VALID_EMAIL_REGEX }, 
                      uniqueness: { case_sensitive: false }

    has_secure_password
    validates :password, length: { minimum: 6 }

    has_many :microposts
end

new.html.slim view:

- provide(:title, 'Sign up')
h1 Sign up
.row
  .col-md-6.col-md-offset-3
    = form_for(@user) do |f|
      = render 'shared/error_messages'
      = f.label :name
      = f.text_field :name, class: 'form-control'
      = f.label :email
      = f.text_field :email, class: 'form-control'
      = f.label :password
      = f.password_field :password, class: 'form-control'
      = f.label :password_confirmation, "Confirmation"
      = f.password_field :password_confirmation, class: 'form-control'
      = f.submit "Create my account", class: "btn btn-primary"

When I try to create a new user, the route changes from signup to users. However, the user is not created, and the errors are:

The form contains 2 errors. Password can't be blank Password is too short (minimum is 6 characters)

However, a password was entered. Do you see anything that would cause this? Thank you.

EDIT:

This is the console output when trying to make a user on the signup page:

Started POST "/users" for 127.0.0.1 at 2014-11-07 15:02:51 -0800
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"DP7+I+/MRBidFqmoav2E8bUAxt+f5kSfPaRPkcEehLE=", "user"=>{"name"=>"Joe Bob", "email"=>"bob@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
Unpermitted parameters: password, password_confirmation
   (0.1ms)  BEGIN
  User Exists (1.6ms)  SELECT  1 AS one FROM `users`  WHERE `users`.`email` = 'msutyak@gmail.com' LIMIT 1
   (0.1ms)  ROLLBACK
  Rendered shared/_error_messages.html.slim (1.1ms)
  Rendered users/new.html.slim within layouts/application (3.3ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 34ms (Views: 13.3ms | ActiveRecord: 1.9ms)

I am not sure why password and password_confirmation are "unpremitted". Any ideas?

Upvotes: 1

Views: 116

Answers (2)

RahulOnRails
RahulOnRails

Reputation: 6542

Make sure you are using Devise latest version and have a look at DeviseStrongParameters

Add to your application controller

before_filter :add_sanitized_params, if: :devise_controller?

def add_sanitized_params
  devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:password, :password_confirmation)}
end

Please let me know if it does not work ...

Upvotes: 1

James Partaker
James Partaker

Reputation: 26

Print out what you are passing into User.new(). If the password is not being passed in, check that you have explicitly allowed it in the strong parameters. ie) params.require(:user).permit(:name, :email, :password)

Upvotes: 1

Related Questions