2012-10-29 74 views
2

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

我在用户表中创建了一个pass_change布尔列,当创建一个新用户时,它是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 

有人能看到我可能是做错了什么?我正在使用设计宝石来处理验证。任何帮助都将不胜感激。提前致谢。

更新:

这里是我的日志,当我试图更改密码:

Started PUT "https://stackoverflow.com/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 

回答

0

尝试,这可能是其对你有所帮助,在写你的模型

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 
+0

我加入你的代码,以我的项目模型,并没有什么改变。我也试过我的用户模型,因为我误解了。 – Jazz