Ruby on Rails: Devise - password change at first login

I have an application that allows users to enter projects into a database where they can search for them later. Each user has his own login, which will be issued by the system administrator. When a user logs in for the first time, I want the "Change password" view to be displayed on the regular home page so that the user can change their password before using the application.

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

Currently, when a user logs in for the first time, they are displayed in the password change window, but when I test this function, the password does not change, and the view remains unchanged after I send a new password and pass_change for this user does not change to true .

Index Type:

 <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 can do wrong? I use a gem to handle authentication. Any help at all would be greatly appreciated. Thanks in advance.

Update:

Here are my logs when I try to change my 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:

 after_update :update_pass_changed def update_pass_changed self.pass_changed = true self.save end 
+2
source share
1 answer

try, it may be useful for you, write in your 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 
0
source

Source: https://habr.com/ru/post/984623/


All Articles