我有一个应用程序,允许用户将项目输入数据库,以后可以在其中搜索它们。每个用户都有自己的登录名,由系统管理员提供。当用户第一次登录时,我希望通常的主页显示“更改密码”视图,以便用户在使用应用程序之前必须更改密码。
我pass_change
在 User 表中设置了一个布尔列,这是false
创建新用户的时间。
目前,当用户第一次登录时,他们被带到了更改密码视图,但是当我测试这个功能时,密码没有改变,并且在我提交新密码后视图保持不变,并且该pass_change
用户不会更改为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
谁能看到我可能做错了什么?我正在使用设计 gem 来处理身份验证。任何帮助都将不胜感激。提前致谢。
更新:
这是我尝试更改密码时的日志:
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)
我的索引视图现在看起来像这样:
<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>
在我的用户模型中,我添加了:
after_update :update_pass_changed
def update_pass_changed
self.pass_changed = true
self.save
end