我正在尝试在rails中实现一个被遗忘的密码解决方案。我有一个表格供用户输入其注册帐户的电子邮件地址,我打算让邮件通过电子邮件向他们发送一个唯一的URL,将他们链接到密码重置页面。
我的 config / routes.rb 文件包含以下路由:
resources :users do
collection do
get :lost_password #the account-email submisison form url
get :reset_password #a url for the function that sends the response email
end
end
当我从控制台运行 rake routes 时,我会得到我想要的路径:
lost_password_users GET /users/lost_password(.:format) {:action=>"lost_password", :controller=>"users"}
reset_password_users GET /users/reset_password(.:format) {:action=>"reset_password", :controller=>"users"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
BUT!当用户点击下面代码中列出的表单上的提交按钮时:
<h3>Reset Password</h3>
<%= form_for(:user, :url => reset_password_users_path) do |f| %>
<p>Enter the email address you used to register for this site.</p></br>
<div class="field">
<%= f.label :email %> </br>
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Send Email" %>
</div>
<% end %>
我得到了
没有路线匹配 “/用户/ reset_password”
错误通过ActionController。
事实上,我确实为 lost_password_users_path 和 reset_password_users_path 定义了视图和控制器功能,所以我很困惑为什么我会碰到这个路由错误。我有两个问题:
提前致谢!
答案 0 :(得分:1)
尝试在routes.rb
中将get :reset_password
更改为post :reset_password
答案 1 :(得分:0)
重置密码功能:希望能正常工作,用于控制器更新功能
if params[:user][:password].present?
puts "present"
puts params[:current_password]
if (params[:user][:password] == "")
params[:user].delete(:password)
else
if @user.valid_password?(params[:current_password])
@updated = true
puts @updated.to_s
@user.update_attributes(user_params)
sign_in(@user,:bypass => true)
flash[:notice] = "Password Updated Successfully"
redirect_back fallback_location: user_url
else
@updated = false
puts @updated.to_s
flash[:danger] = "Current Password does not matched"
redirect_back fallback_location: user_url
end
end