Ruby on Rails: Devise - password change on first l

2019-07-21 13:22发布

问题:

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.

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

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.

Index view:

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

Application helper:

module ApplicationHelper

    def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

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

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.

Update:

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

回答1:

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

or

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