我是新手。我正在创建类似克隆应用程序的Twitter。我正在为我的应用程序添加忘记密码功能。从互联网上按照步骤进行操作之后。我收到路由错误。我尝试了所有可能的方法来解决它。但它仍然显示相同。我尝试过rake route命令,但我认为路线没有问题。
Rake routes:
password_resets POST /password_resets(.:format) password_resets#create
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PUT /password_resets/:id(.:format) password_resets#update
cancel_request PUT /requests/:id/cancel(.:format)
edit.html.erb
<h1>Reset Password</h1>
//here I tried changing to password_resets_path still the same error occurs.
<%= form_for @user, url: password_reset_path(params[:id]) do |f| %>
<% if @user.errors.any? %>
<div class='error_messages'>
<h2>Form is invalid</h2>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class='field'>
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class='field'>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class='actions'><%= f.submit 'Update password' %></div>
<% end %>
password_reset controller:
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
flash[:notice] = 'E-mail sent with password reset instructions.'
redirect_to new_session_path
end
def edit
@user = User.find_by_password_reset_token!(params[:id])
end
def update
@user = User.find_by_password_reset_token!(params[:id])
if @user.password_reset_sent_at < 2.hour.ago
flash[:notice] = 'Password reset has expired'
redirect_to new_password_reset_path
elsif @user.update(user_params)
flash[:notice] = 'Password has been reset!'
redirect_to new_session_path
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:password)
end
//调用password_reset控制器
<%=链接到“忘记密码?”,password_resets_path%>
我在stackoverflow上发现了类似的问题,但没有一个回答我的问题。尝试了所有现有答案。
答案 0 :(得分:0)
问题出在我指定的错误路径上,对于password_resets_path,new_password_reset是正确的路径