2012-02-29 3 views
0

레일즈 앱에서 사용자 테이블을 :email:hashed_password 문자열로 설정합니다. 그러나 암호 재설정 옵션을 제공하고 싶습니다. Railscast 274 다음에 모든 항목이 설정되어 있지만 실제 비밀번호는 내가 재설정 한 것과 같이 변경되지 않습니다. 내가 유효성을 처리하지만 :password 다른 곳에서 일하는 내 사용자 모델에서 :password:hashed_password으로 변경해야한다고 생각했기 때문에 배제했습니다. 누군가 나를 알아낼 수있게 도와 줄 수 있습니까?사용자가 hashed_password를 설정 한 비밀번호 재설정

여기 내 User 모델의 :

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :email, :password, :password_confirmation  

    before_save :encrypt_new_password 
    before_create { generate_token(:auth_token) } 
    before_validation :downcase_email 

    has_one :profile, :dependent => :destroy 

    accepts_nested_attributes_for :profile 

    validates :email, :uniqueness => true, 
       :length => { :within => 5..50 }, 
       :format => { :with => /^[^@][\w.-][email protected][\w.-]+[.][a-z]{2,4}$/i } 
    validates :password, :confirmation => true, 
        :length => { :within => 4..20 }, 
        :presence => true, 
        :if => :password_required? 

    def self.authenticate(email, password) 
    user = find_by_email(email) 
    return user if user && user.authenticated?(password) 
    end 

    def authenticated?(password) 
    self.hashed_password == encrypt(password) 
    end 

    def send_password_reset 
    generate_token(:password_reset_token) 
    self.password_reset_sent_at = Time.zone.now 
    save! 
    UserMailer.password_reset(self).deliver 
    end 

    def generate_token(column) 
    begin 
     self[column] = SecureRandom.hex 
    end while User.exists?(column => self[column]) 
    end 
end 

password_resets_controller :

class PasswordResetsController < ApplicationController 
    layout "password_reset" 

    def new 
    end 

    def create 
    user = User.find_by_email(params[:email]) 
    if user 
     user.send_password_reset 
     redirect_to new_password_reset_path, :notice => "Check your email for password reset instructions." 
    else 
     redirect_to new_password_reset_path, :notice => "Sorry, we couldn't find that email. Please try again." 
    end 

    end 

    def edit 
    @user = User.find_by_password_reset_token!(params[:id]) 
    session[:user_id] = @user.id 
    end 

    def update 
    @user = User.find_by_password_reset_token!(params[:id]) 
    if @user.password_reset_sent_at < 2.hours.ago 
     redirect_to new_password_reset_path, :alert => "Your password reset link has expired." 
    elsif @user.update_attributes(params[:user]) 
     redirect_to profile_path(@user), :notice => "Great news: Your password has been reset." 
    else 
     render :edit 
    end 
    end 
end 

내 password_reset 양식 코드 :

<%= form_for @user, :url => password_reset_path(params[:id]) do |f| %> 
    <% if @user.errors.any? %> 
    <div id="error_messages"> 
    <% for message in @user.errors.full_messages %> 
    <li class="error"><%= message %></li> 
    <% end %> 
    </div> 
    <% end %> 

    <p class="label"><label for="password">New Password:</label></p> 
    <p><%= password_field_tag :password, nil, :autofocus => true, :autocomplete => 'off', :placeholder => 'Enter a new password' %></p> 
    <p class="label"><label for="password_confirmation">Confirm Password:</label></p> 
    <p><%= password_field_tag :password_confirmation, nil, :autofocus => false, :autocomplete => 'off', :placeholder => 'Reenter your new password' %></p> 

    <%= submit_tag 'Update Password', :class => 'button orange' %> 

<% end %> 

가 내 users_controller 업데이트 작업 :

def update 
    @user = current_user 
    if @user.update_attributes(params[:user]) 
    redirect_to settings_path, :notice => 'Updated user information successfully.' 
    else 
    render :action => 'edit' 
    end 
end 

내 서버 메시지 내 암호를 업데이트 :

Started POST "/password_resets/56be9c1168637d64eaa42b2551ef9b6c" for 127.0.0.1 at Thu Mar 08 19:36:03 -0500 2012 
    Processing by PasswordResetsController#update as HTML 
    Parameters: {"commit"=>"Update Password", "password_confirmation"=>"[FILTERED]", "authenticity_token"=>"N8v69nP9NbaGVGdYwmv6EF8uIp4/Vqld/y+Q2K40CMQ=", "utf8"=>"\342\234\223", "id"=>"56be9c1168637d64eaa42b2551ef9b6c", "password"=>"[FILTERED]"} 

답변

2

그것은 당신이 password_field_tag를 사용하고 있기 때문에 모양, 매개 변수가 무시 될 때 사용자 모델이 업데이트됩니다. 암호 매개 변수가 params[:user]에 중첩되도록 f.password_field을 대신 사용해보십시오.

또는 PasswordResetsController에서 if @user.update_attributes(:password => params[:password], :password_confirmation => params[:password_confirmation])을 호출 할 수도 있습니다.

+0

그랬습니다. 얼마나 오래 알아 내려고 노력했는지 모르실 겁니다. 'password_field_tag'와 params에 대해 더 자세히 읽을 수있는 곳이 있습니까? – tvalent2

+0

굉장! 도움이 된 것을 기쁘게 생각합니다. 다음은 [password_field] (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-password_field)의 API 문서 링크입니다. – manafire

+0

감사합니다. 나는 왜 그런 생각이 없지만 문서는 대부분 나에게 혼란 스럽다. 그들은 단지 초보자를 위해 작성된 것 같지 않습니다. 이제 JS가 모바일 MIME 유형으로 작동하지 않는 이유를 알아낼 수만 있다면! – tvalent2

1

submit_tag를 f.submit으로 변경하는 것을 잊지 마십시오.

관련 문제