I get a NoMethodError when submitting this form, I've no idea how to get it to work. It's just a password reset form though the Devise gem, and trying the @user.send_reset_password_instructions
in the console works just fine.
我在提交此表单时收到NoMethodError,我不知道如何让它工作。它只是一个密码重置形式,通过Devise gem,并在控制台中尝试@ user.send_reset_password_instructions工作得很好。
Here's the form in my view:
这是我视图中的表单:
<%= form_tag reset_password_path(:email), :id => 'forgotten_password' do %>
<%= email_field_tag :email, nil, :placeholder => 'Email address' %>
<%= submit_tag "Reset password" %>
<% end %>
In my routes:
在我的路线:
get "/dashboard/reset_password/:email" => "docs#reset_password", :as => :reset_password
In my controller:
在我的控制器中:
def reset_password
@user = User.where(:email => params[:email]).first
@user.send_reset_password_instructions
end
Here's the error:
这是错误:
No route matches [POST] "/dashboard/reset_password/email"
3 个解决方案
#1
2
Your route needs to have the POST method in it, not GET. And it should be /email
not /:email
你的路线需要有POST方法,而不是GET。它应该是/电子邮件不是/:电子邮件
post "/dashboard/reset_password/email" => "docs#reset_password", :as => :reset_password
#2
1
You defined your route to respond to GET
requests, and your form is sending a POST
request. Try changing the method of your route from get
to post
您定义了路由以响应GET请求,并且您的表单正在发送POST请求。尝试更改路线从get到post的方法
#3
0
So, part one is to make your route a POST instead of a GET (as mentioned previously). Typically whenever you're sending data to the server and it's causing a change (like setting the reset password token on a user account and sending them an email) you use a POST.
因此,第一部分是使您的路由成为POST而不是GET(如前所述)。通常,只要您将数据发送到服务器并导致更改(例如在用户帐户上设置重置密码令牌并向其发送电子邮件),就可以使用POST。
Change your route to look like this:
将您的路线更改为如下所示:
POST "/dashboard/reset_password" => "docs#reset_password", :as => :reset_password
Note: I also dropped the :email part of the path. You don't really need that.
注意:我还删除了路径中的:email部分。你真的不需要那个。
Part two, you'll need to tweak your form a little. You don't need to pass the :email symbol to the path. I'm not actually sure what that will do for you in this case. You get the email in the params hash in the controller (params[:email]) because you've got an email field in your form.
第二部分,你需要稍微调整你的表格。您无需将:email符号传递给路径。在这种情况下,我真的不确定那会对你有什么影响。您可以通过控制器(params [:email])中的params哈希获得电子邮件,因为您的表单中有一个电子邮件字段。
Change your form to look like this:
将表单更改为如下所示:
<%= form_tag reset_password_path, :id => 'forgotten_password' do %>
<%= email_field_tag :email, nil, :placeholder => 'Email address' %>
<%= submit_tag "Reset password" %>
<% end %>
That should generate the POST form path '/dashboard/reset_password' in your form HTML. When the user clicks on the "Reset password" button on the page the :email field in the form will get submitted to you. Rails will stuff it into the params and your controller method 'reset_password' be able to read it just fine.
这应该在表单HTML中生成POST表单路径'/ dashboard / reset_password'。当用户点击页面上的“重置密码”按钮时,表单中的:email字段将提交给您。 Rails会把它塞进params,你的控制器方法'reset_password'能够很好地读取它。
You might also want to do some error catching in your controller method. The most common problem with email password reset is that there's a typo in the email and you won't be able to find the user. It's helpful to return a message to the user when that happens.
您可能还想在控制器方法中捕获一些错误。电子邮件密码重置最常见的问题是电子邮件中存在拼写错误,您将无法找到该用户。发生这种情况时,向用户返回消息会很有帮助。
Good luck!
#1
2
Your route needs to have the POST method in it, not GET. And it should be /email
not /:email
你的路线需要有POST方法,而不是GET。它应该是/电子邮件不是/:电子邮件
post "/dashboard/reset_password/email" => "docs#reset_password", :as => :reset_password
#2
1
You defined your route to respond to GET
requests, and your form is sending a POST
request. Try changing the method of your route from get
to post
您定义了路由以响应GET请求,并且您的表单正在发送POST请求。尝试更改路线从get到post的方法
#3
0
So, part one is to make your route a POST instead of a GET (as mentioned previously). Typically whenever you're sending data to the server and it's causing a change (like setting the reset password token on a user account and sending them an email) you use a POST.
因此,第一部分是使您的路由成为POST而不是GET(如前所述)。通常,只要您将数据发送到服务器并导致更改(例如在用户帐户上设置重置密码令牌并向其发送电子邮件),就可以使用POST。
Change your route to look like this:
将您的路线更改为如下所示:
POST "/dashboard/reset_password" => "docs#reset_password", :as => :reset_password
Note: I also dropped the :email part of the path. You don't really need that.
注意:我还删除了路径中的:email部分。你真的不需要那个。
Part two, you'll need to tweak your form a little. You don't need to pass the :email symbol to the path. I'm not actually sure what that will do for you in this case. You get the email in the params hash in the controller (params[:email]) because you've got an email field in your form.
第二部分,你需要稍微调整你的表格。您无需将:email符号传递给路径。在这种情况下,我真的不确定那会对你有什么影响。您可以通过控制器(params [:email])中的params哈希获得电子邮件,因为您的表单中有一个电子邮件字段。
Change your form to look like this:
将表单更改为如下所示:
<%= form_tag reset_password_path, :id => 'forgotten_password' do %>
<%= email_field_tag :email, nil, :placeholder => 'Email address' %>
<%= submit_tag "Reset password" %>
<% end %>
That should generate the POST form path '/dashboard/reset_password' in your form HTML. When the user clicks on the "Reset password" button on the page the :email field in the form will get submitted to you. Rails will stuff it into the params and your controller method 'reset_password' be able to read it just fine.
这应该在表单HTML中生成POST表单路径'/ dashboard / reset_password'。当用户点击页面上的“重置密码”按钮时,表单中的:email字段将提交给您。 Rails会把它塞进params,你的控制器方法'reset_password'能够很好地读取它。
You might also want to do some error catching in your controller method. The most common problem with email password reset is that there's a typo in the email and you won't be able to find the user. It's helpful to return a message to the user when that happens.
您可能还想在控制器方法中捕获一些错误。电子邮件密码重置最常见的问题是电子邮件中存在拼写错误,您将无法找到该用户。发生这种情况时,向用户返回消息会很有帮助。
Good luck!