0

I'm trying to change the user password using the gem bcrypt and the hash-salt method.

Here's my code where i include my attempt to change password, but it gives me an error of a missing template.

User Controller

def create

    @user = User.new(user_params)

end

def change_password
    @user = User.find(params[:id])
    if @user.password_hash == BCrypt::Engine.hash_secret(params[:current_password], @user.password_salt)
      @user.password = params[:password]
      @user.save
      redirect_to "/users/#{@user.id}"
    end
end
private

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

User Model

before_save :encrypt_password


def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        return user
    else
        return nil
    end
end


def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
end

also, the Routes

patch 'users/:id/change_password' => 'users#change_password'
resources :users

and last but not less important, the form.

<%= form_for(@user, :url => "change_password") do |f| %>

  <%= hidden_field(:user, :email, :value => @user.email) %>

  <div class="form-group">
    <div class="form-group col-md-4"><%= f.label :contraseña_actual %></div>
    <div class="form-group col-md-8"><%= f.password_field(:current_password, :class => "form-control") %></div>
  </div>

    <div class="form-group col-md-4"><%= f.label :nueva_contraseña %></div>
    <div class="form-group col-md-8"><%= f.password_field(:password, :class => "form-control") %></div>

  <div class="form-group">
    <div class="form-group col-md-4"><%= f.label :confirmar_contraseña %></div>
    <div class="form-group col-md-8"><%= f.password_field(:password_confirmation, :class => "form-control") %></div>    
  </div>

  <div class="col-md-offset-2 col-md-10">
    <button type="submit" class="btn btn-default">Cambiar Contraseña</button>
  </div>
<% end %>
  • So what's the problem? – Mike G May 20 '15 at 16:20
  • alrdy edited the post, i got an error when trying to change the password, this is the error on my attempt Missing template users/change_password, application/change_password with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/app/views" * ".rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/devise-3.4.1/app/views" – Felipe Eduardo Rojas May 20 '15 at 16:45

0 Answers0