Rails - 如何重置密码(不通过电子邮件确认)

时间:2022-10-24 19:21:19

I'm new to rails and I want to offer users the feature to change password in their user page. Requiring their old password and setting a new one. However, I have no idea how to accomplish this.

我是rails的新手,我想为用户提供更改用户页面密码的功能。需要他们的旧密码并设置一个新密码。但是,我不知道如何做到这一点。

There is a Railscasts episode on resetting password through email but I dont want to do it with email.

有一个关于通过电子邮件重置密码的Railscasts剧集,但我不想用电子邮件来做。

I generated a Password Update controller. But I know I am making a terrible mistake. Hopefully you guys can point it out. And hopefully this question wasn't too confusing.

我生成了一个密码更新控制器。但我知道我犯了一个可怕的错误。希望你们能指出来。希望这个问题不会太混乱。

password updates controller

密码更新控制器

  class PasswordUpdateController < ApplicationController
  def new
  end

  def update
  end

  def show
    @user = User.find(params[:id]) 
  end
  end

new password_update

 %h1
 = form_for @user, :url => password_update_path(params[:id]) do |f|
   .field
     = f.label :old_password
     = f.password_field :password
   .field
     = f.label :password
     = f.password_field :password
   .field
     = f.label :password_confirmation
     = f.password_field :password_confirmation
   .actions
     = f.submit "Update Password"

Routing Error No route matches [POST] "/password_update/1"

路由错误没有路由匹配[POST]“/ password_update / 1”

routes.rb

TootApp::Application.routes.draw do

get "sessions/new"

get "static_pages/home"

get "static_pages/help"

get "password_updates/new"

resources :sessions

resources :products

resources :photos

resources :password_update

1 个解决方案

#1


3  

That's not how you should use controllers. Password update should be an 'action' within a 'controller' and that controller, when it comes to user credentials, should rightfully be in the UsersController, where an 'update' action takes in parameters that you post from a form:

那不是你应该如何使用控制器。密码更新应该是“控制器”中的“操作”,当涉及到用户凭据时,该控制器应该正确地位于UsersController中,其中“更新”操作接受您从表单发布的参数:

class UsersController < ApplicationController
  def update
    @user = User.find(params[:id])
    @user.update_attributes(params[:user])
    ....
  end
end

And in your HTML form, you can just specify:

在HTML表单中,您只需指定:

= form_for @user do |f|
  ...

without even needing to specify the URL, since rails will implicitly provide you with the right URL in the background :)

甚至不需要指定URL,因为rails将在后台隐式地为您提供正确的URL :)

And make sure to have your 'Routes' correctly setup like so:

并确保您的'路线'正确设置如下:

resources :users

It basically sets up the RESTful routes for users. You can find out the routes it generates, by running the following in your console:

它基本上为用户设置RESTful路由。您可以通过在控制台中运行以下命令来查找它生成的路由:

rake routes

Apart from hooking up your own user credential management features, why not try out the devise gem by Jose Valim, and pair it with SimpleForm gem.

除了连接自己的用户凭证管理功能外,为什么不试试Jose Valim的设计宝石,并将其与SimpleForm gem配对。

Hope this helps!

希望这可以帮助!

#1


3  

That's not how you should use controllers. Password update should be an 'action' within a 'controller' and that controller, when it comes to user credentials, should rightfully be in the UsersController, where an 'update' action takes in parameters that you post from a form:

那不是你应该如何使用控制器。密码更新应该是“控制器”中的“操作”,当涉及到用户凭据时,该控制器应该正确地位于UsersController中,其中“更新”操作接受您从表单发布的参数:

class UsersController < ApplicationController
  def update
    @user = User.find(params[:id])
    @user.update_attributes(params[:user])
    ....
  end
end

And in your HTML form, you can just specify:

在HTML表单中,您只需指定:

= form_for @user do |f|
  ...

without even needing to specify the URL, since rails will implicitly provide you with the right URL in the background :)

甚至不需要指定URL,因为rails将在后台隐式地为您提供正确的URL :)

And make sure to have your 'Routes' correctly setup like so:

并确保您的'路线'正确设置如下:

resources :users

It basically sets up the RESTful routes for users. You can find out the routes it generates, by running the following in your console:

它基本上为用户设置RESTful路由。您可以通过在控制台中运行以下命令来查找它生成的路由:

rake routes

Apart from hooking up your own user credential management features, why not try out the devise gem by Jose Valim, and pair it with SimpleForm gem.

除了连接自己的用户凭证管理功能外,为什么不试试Jose Valim的设计宝石,并将其与SimpleForm gem配对。

Hope this helps!

希望这可以帮助!