Ruby on rails 传递参数以将_链接到内部表单_(或如何通过sms重置设计密码)
我的页面上的用户可以用他们的电话号码注册。如果他们忘记了密码,应该通过短信重新发送 我的目标是在我的registrations/new.html.haml页面上有一个链接,该链接将触发一些自定义控制器(该控制器发送带有密码的短信) 我曾想过更换Desive的密码_controller.rb编辑操作,但似乎没有触发:Ruby on rails 传递参数以将_链接到内部表单_(或如何通过sms重置设计密码),ruby-on-rails,ruby,devise,ruby-on-rails-5,Ruby On Rails,Ruby,Devise,Ruby On Rails 5,我的页面上的用户可以用他们的电话号码注册。如果他们忘记了密码,应该通过短信重新发送 我的目标是在我的registrations/new.html.haml页面上有一个链接,该链接将触发一些自定义控制器(该控制器发送带有密码的短信) 我曾想过更换Desive的密码_controller.rb编辑操作,但似乎没有触发: class Front::Users::PasswordsController < Devise::PasswordsController # GET /resource/
class Front::Users::PasswordsController < Devise::PasswordsController
# GET /resource/password/new
def new
p "hello"
super
end
end
所以,基本上我现在需要的是以某种方式将:phone参数从这个表单传递到link_到helper。我尝试了谷歌搜索“rails传递参数以链接到”的所有结果,但在调用我的profile_控制器时
class Front::ProfileController < FrontController
def change_password
logger.debug "This is Patrick with params: #{params}"
end
end
class Front::ProfileController
参数不包含电话号码。如何通过传递参数或其他方式实现所需的功能?您需要这样的功能吗
link_to 'Send me the pass', send_pass_path(@user, param: 'foo')
此外,设计使用
BCrypt
加密密码。BCrypt是一个散列函数,因此无法获取真实密码。如果需要,您可以重新生成一个新密码并通过短信发送新密码,但不能发送旧的真实密码。您需要这样的密码吗
link_to 'Send me the pass', send_pass_path(@user, param: 'foo')
此外,设计使用
BCrypt
加密密码。BCrypt是一个散列函数,因此无法获取真实密码。如果需要,您可以重新生成新密码并通过短信发送新密码,但您不能发送旧的真实密码。搜索解决方案让我找到了,这就是我最终解决问题的方法
我的最终表格代码:
=form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
.section.section-inset-1
.container
.row
.col-xs-12.col-md-8.col-md-offset-2
%section
%h4 Sign in
.rd-mailform.row.flow-offset-6
.col-xs-12.col-sm-6
=f.text_field :phone, placeholder: "Phone number", data: { constraints: "@NotEmpty" }, autofocus: true, id: "standard_order_phone"
.col-xs-12.col-sm-6
=f.password_field :password, placeholder: "Password", data: { constraints: "@Password" }, class: "form-input", autocomplete: "off"
.col-xs-12.col-sm-6.text-left
%label.mfCheckbox
=f.check_box :remember_me, class: 'mfCheckbox__input'
%span Remember me
.col-xs-12.col-sm-6.text-right
#send_password
=submit_tag 'Send password per SMS', class: "reset_password", id: 'reset_btn', name: 'reset', remote: true
%section.section-inset-3
.row.offset-5
.col-xs-12
=f.submit 'Sign in', class: 'btn btn-sm btn-min-width btn-success'
%section.section-inset-3
.row.ud_mt_4em
%h6 No account yet?
.row.offset-5
=link_to 'Create one!', new_user_registration_path, method: :get, class: 'btn btn-sm btn-min-width btn-success-mod-1'
然后,我扩展Desive会话控制器:
class Front::Users::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
def new
if params[:reset]
phone = params[:user][:phone]
# logger.debug("Phone is: #{phone}")
new_password = Array.new
5.times do
new_password << rand(9)
end
password = new_password.join("")
User.where(phone: phone).update(password: password)
stripped_phone = phone.gsub(/\s+/, "").gsub(/[()]/, "").gsub(/-/, "")
encoded_phone = URI.escape(stripped_phone)
encoded_message = URI.escape("Your new password - #{password}.")
# logger.debug("New password for user #{phone} - #{password}")
# Some code for sending SMS
# doc = Nokogiri::HTML(open(url))
flash[:success] = "New password sent per SMS."
redirect_to new_user_session_path
else
super
end
end
end
class-Front::Users::sessioncontroller 新密码搜索解决方案让我找到了解决方案,这就是我最终解决问题的方法
我的最终表格代码:
=form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
.section.section-inset-1
.container
.row
.col-xs-12.col-md-8.col-md-offset-2
%section
%h4 Sign in
.rd-mailform.row.flow-offset-6
.col-xs-12.col-sm-6
=f.text_field :phone, placeholder: "Phone number", data: { constraints: "@NotEmpty" }, autofocus: true, id: "standard_order_phone"
.col-xs-12.col-sm-6
=f.password_field :password, placeholder: "Password", data: { constraints: "@Password" }, class: "form-input", autocomplete: "off"
.col-xs-12.col-sm-6.text-left
%label.mfCheckbox
=f.check_box :remember_me, class: 'mfCheckbox__input'
%span Remember me
.col-xs-12.col-sm-6.text-right
#send_password
=submit_tag 'Send password per SMS', class: "reset_password", id: 'reset_btn', name: 'reset', remote: true
%section.section-inset-3
.row.offset-5
.col-xs-12
=f.submit 'Sign in', class: 'btn btn-sm btn-min-width btn-success'
%section.section-inset-3
.row.ud_mt_4em
%h6 No account yet?
.row.offset-5
=link_to 'Create one!', new_user_registration_path, method: :get, class: 'btn btn-sm btn-min-width btn-success-mod-1'
然后,我扩展Desive会话控制器:
class Front::Users::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
def new
if params[:reset]
phone = params[:user][:phone]
# logger.debug("Phone is: #{phone}")
new_password = Array.new
5.times do
new_password << rand(9)
end
password = new_password.join("")
User.where(phone: phone).update(password: password)
stripped_phone = phone.gsub(/\s+/, "").gsub(/[()]/, "").gsub(/-/, "")
encoded_phone = URI.escape(stripped_phone)
encoded_message = URI.escape("Your new password - #{password}.")
# logger.debug("New password for user #{phone} - #{password}")
# Some code for sending SMS
# doc = Nokogiri::HTML(open(url))
flash[:success] = "New password sent per SMS."
redirect_to new_user_session_path
else
super
end
end
end
class-Front::Users::sessioncontroller 新密码是的,我需要这样的内容,但是如何从表单中提取f.text\u字段:phone
-值?模型中有字段:phone
?如果是,那应该可以,如果不是,你应该使用text\u field\u tag'phone',@phone\u value
我将我的链接改为=link\u改为'Send password via SMS',重置密码路径(@user,phone:params[:phone]),方法::get
,但仍然没有收到我的参数散列中的参数params=>“get”,“authenticity\u token”=>“ze8PgJinJuiIsROfAHca+fU+Owp0IKtXBkJw0R1ik6f+S0E2zB4aOvj5jdq8CGkwib8IlN8RascVIFZdIdJqFw=”,“controller”=>“front/profile”,“action”=>“change\u password”}允许:false>
它是如何生成链接的?我是指正在创建的链接的html。尝试重置密码路径(user\u id:@user.id,phone:params[/code>(或id
,我不知道它在你的中是什么。link_to
的第一个参数是数据,第二个是选项。然后,路径的所有参数都是@user only,第二个(phone
)是指向选项的链接(未知选项)是的,我需要这样的东西,但是我如何从表单中提取f.text\u field:phone
-值?模型中有:phone
?如果是,应该可以,如果不是,你应该使用text\u field\u标签'phone',@phone\u value
我将链接改为=链接改为'Send password via SMS',重置密码路径(@user,phone:params[:phone]),method::get
但仍然没有收到我的参数散列中的参数params=>“get”,“authenticity\u token”=>“ze8PgJinJuiIsROfAHca+fU+Owp0IKtXBkJw0R1ik6f+S0E2zB4aOvj5jdq8CGkwib8IlN8RascVIFZdIdJqFw=”,“controller”=>“front/profile”,“action”=>“change\u密码”“}允许:false>
链接是如何生成的?我是指链接到
正在创建的html。尝试重置密码路径(user\u id:@user.id,phone:params[:phone])
(或id
,我不知道它在你的中是什么。link_to
的第一个参数是数据,第二个是选项。然后,路径的所有参数都是@user only,第二个(phone
)是指向选项的链接(未知选项)