2012-10-29 97 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