Ruby on Rails:Devise - 首次登录时密码更改 [英] Ruby on Rails: Devise - password change on first login

查看:458
本文介绍了Ruby on Rails:Devise - 首次登录时密码更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户将项目输入数据库,以后可以在其中搜索。每个用户都有自己的登录名,这将由系统管理员发出。当用户第一次登录时,我希望通常的主页显示更改密码视图,以便用户在使用该应用之前必须更改密码。

I have an app that allows users to enter projects into a database, where they can search for them later. Each user has their own login, which will be given out by a system admin. When the user logs in for the first time, I want the usual homepage to show a "Change your password" view, so that the user has to change their password before using the app.

我已经在用户表中设置了一个 pass_change 布尔列,当一个新用户是 false 创建。

I have set up a pass_change boolean column in the User table, which is false when a new user is created.

目前,当用户第一次登录时,会将其带入更改密码视图,但是当我测试此功能时,密码提交新密码后,视图不会更改,视图保持不变,而且该用户的 pass_change 不会更改为 true

At the moment, when the user logs in for the first time, they are brought to the change password view, but when I test this function, the password doesn't change and the view stays the same after I submit the new password, and the pass_change for that user doesn't change to true.

索引视图

<html>
<% if current_user.pass_changed == true %>

  <%= stylesheet_link_tag    "index"%>
<body>

<div id = "section1">
<div class = "h1">Database Search</div>

    <div class = "h3">What would you like to do?</div>



<div class="new_project_button">
<%= button_to "Create New Project", new_project_path, :class => "button", :method => "get" %>
</div>
<div class="search_button">
<%= button_to "      Search      ", search_path, :class => "button", :method => "get" %>
</div>
</div>


</body>

<%else%>


<div class ="title">Change your password</div><%= current_user.pass_changed %>


<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= devise_error_messages! %>

<div class = "signin">


<div class = "field">New Password: 
  <%= f.password_field :password, :class => "password" %>
  </div>

  <div class = "field">Confirm: 
  <%= f.password_field :password_confirmation, :class => "password" %>
  </div>

                        <%= f.hidden_field :pass_changed, :value => true %>
  <div class = "signup_button"><%= f.submit "Change my password", :class => "button" %>
  </div>

</div>
<% end %>
<% end %>


</html> 

应用程序助手:

module ApplicationHelper

    def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

任何人都可以看到我可能做错了什么?我正在使用devise gem处理身份验证。任何帮助将是非常感谢。感谢提前。

Can anyone see what I might be doing wrong? I am using the devise gem to handle authentication. Any help at all would be much appreciated. Thanks in advance.

更新:

当我尝试更改密码时,这是我的日志: p>

Here are my logs when I try to change the password:

Started PUT "/users/password" for 127.0.0.1 at 2012-10-29 14:15:05 +0000
Processing by Devise::PasswordsController#update as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"eBU5jvN6C+JSIZNmsEaxUyydrvPRjtGZeWLxlQzFJKI=", "user"=>{"password"=
>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "pass_changed"=>"true"}, "commit"=>"Change my password"}
  ←[1m←[35mUser Load (0.0ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2012-10-29 14:15:05 +0000
Processing by ProjectsController#index as HTML
  ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1←[0m
  ←[1m←[35mProject Load (0.0ms)←[0m  SELECT "projects".* FROM "projects"
  Rendered projects/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 16ms (Views: 15.6ms | ActiveRecord: 0.0ms)

我的索引视图现在看起来像这样:

My index view now looks like this:

<html>
<% if current_user.pass_changed == true %>

  <%= stylesheet_link_tag    "index"%>
<body>

<div id = "section1">
<div class = "h1">Database Search</div><%= current_user.pass_changed %>

    <div class = "h3">What would you like to do?</div>



<div class="new_project_button">
<%= button_to "Create New Project", new_project_path, :class => "button", :method => "get" %>
</div>
<div class="search_button">
<%= button_to "      Search      ", search_path, :class => "button", :method => "get" %>
</div>
</div>


</body>

<%else%>

<%= form_for(current_user, :as => :user, :url => password_path(current_user), :html => { :method => :put }) do |f| %>
  <%= devise_error_messages! %>

<div class = "signin">


<div class = "field">New Password: 
  <%= f.password_field :password, :class => "password" %>
  </div>

  <div class = "field">Confirm: 
  <%= f.password_field :password_confirmation, :class => "password" %>
  </div>

                        <%= f.hidden_field :pass_changed, :value => true %>
  <div class = "signup_button"><%= f.submit "Change my password", :class => "button" %>
  </div>

</div>
<% end %>
<% end %>


</html> 

在我添加的用户模型中有:

and in my user model I added have:

after_update :update_pass_changed

def update_pass_changed
  self.pass_changed = true
  self.save
end


推荐答案

尝试这可能对您有帮助, p>

try this may be it helpful for you, write in you model

after_create :update_pass_change

def update_pass_change
  self.pass_change = true
  self.save
end

def update_pass_change
  self.update_attributes(:pass_change => true)
end

这篇关于Ruby on Rails:Devise - 首次登录时密码更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆