2014-01-15 3 views
1

사용자가 암호를 변경할 수 있지만 이전 암호를 입력해야하는 기본 양식을 만들려고합니다. 사용자의 이전 암호를 확인하는 데 문제가 있습니다. 매번 오래된 암호를 입력 할 때마다 암호가 일치하지 않는다고합니다. authenticate 필드의 실제 암호를 바꾸면 작동합니다. 입력 한 이전 암호를 확인하기 위해 양식에 입력 된 내용을 가져올 수 있습니까?컨트롤러에 양식 값 가져 오기

형태 :

<%= form_for(@user, :url => change_password_action_path(current_user.id), html: { "role" => "form" }) do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 

<div class="form-group"> 
    <%= f.label :old_password, "Old Password:", :class => "control-label" %> 
    <%= f.password_field :old_password, :class => "form-control" %> 
</div> 

<div class="form-group"> 
    <%= f.label :password, "New Password:", :class => "control-label" %> 
    <%= f.password_field :password, :class => "form-control" %> 
</div> 

<div class="form-group"> 
    <%= f.label :password_confirmation, "Password Confirmation:", :class => "control-label" %> 
    <%= f.password_field :password_confirmation, :class => "form-control" %> 
</div> 

<%= f.submit "Update Password", class: "btn btn-large btn-primary" %> 

컨트롤러

def change_password 
    @user = User.find(current_user.id) 
end 

def change_password_action 
    user = current_user.id 
    if User.find(user).authenticate(params[:old_password]) == false 
     flash[:danger] = "Password Doesnt Match: " 
    else 
     flash[:success] = "Password Match" 
      # Validate the new and confirm password. 
    end 
    redirect_to action: :change_password 
end 

경로

get '/change_password' => 'main#change_password' 
patch '/change_password_action' => 'main#change_password_action' 
,개

레일 서버 로그 당신이 당신을 인증 방법에 잘못된 매개 변수를 전달하는 것 같습니다

Started PATCH "/change_password_action.1" for 127.0.0.1 at 2014-01-15 09:04:38 -0600 
Processing by MainController#change_password_action as 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yYdUx37Q7alr3SccuMVjPwCJoMgMPOaiKTesSsILlP4=", "user"=>{"old_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update Password"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'fc1baf63bac072bfefd5ed27664ece5427ad9e64' LIMIT 1 
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"yYdUx37Q7alr3SccuMVjPwCJoMgMPOaiKTesSsILlP4=", "user"=>{"old_password"=>"test123", "password"=>"", "password_confirmation"=>""}, "commit"=>"Update Password", "controller"=>"main", "action"=>"change_password_action", "format"=>"1"} 
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
Redirected to http://localhost:3000/change_password 
Completed 302 Found in 115ms (ActiveRecord: 0.7ms) 


Started GET "/change_password" for 127.0.0.1 at 2014-01-15 09:04:39 -0600 
Processing by MainController#change_password as HTML 
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'fc1baf63bac072bfefd5ed27664ece5427ad9e64' LIMIT 1 
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
Rendered shared/_error_messages.html.erb (0.1ms) 
Rendered main/change_password.html.erb within layouts/application (2.6ms) 
Rendered layouts/_header.html.erb (0.5ms) 
Rendered layouts/_footer.html.erb (0.0ms) 
Completed 200 OK in 19ms (Views: 16.2ms | ActiveRecord: 0.4ms) 
+0

컨트롤러에서 인증을 시도하기 전에'logger.info params.inspect'를 추가하십시오. 그러면 모든 매개 변수가 로그 파일에 저장됩니다. –

+0

요청할 때 웹 서버 콘솔의 출력을 표시하십시오. – Beartech

+0

콘솔에서 무엇이 나오는지 추가했습니다. – evanvee

답변

2

. params [: old_password] 대신 params [: user] [: old_password]를 사용해보세요.

사용자의 form_for가 사용자 개체를 사용하기 때문에 원하는 매개 변수 값은 : user 키 아래에 있습니다. 당신의 PARAMS 해시의 사용자 PARAM이 어디

당신은 또한 당신의 서버 로그에서 볼 수 있습니다 "사용자"=> { "OLD_PASSWORD"=> "[FILTERED]", "암호"=> "[FILTERED] ","password_confirmation "=>"[FILTERED] "}

+0

그걸 고쳐 줘! 고맙습니다!! – evanvee