Rails 表单不在 POST 上调用创建控制器方法
我的 /app/views/password_resets/new.html.erb 视图中有以下表单
<% form_tag password_resets_path do %>
<label><%= t(:email) %>:</label><br />
<%= text_field_tag "email" %><br />
<br />
<%= submit_tag t("reset_password") %>
<% end %>
以及一个名为 PasswordResetsController 的控制器,其中包含一个创建方法:
def create
@user = User.find_by_email(params[:email])
if @user
@user.deliver_password_reset_instructions!
self.notice = t("password_reset_instructions_are_mailed")
redirect_to root_url
else
flash[:error] = t("no_user_found")
render :action => :new
end
end
当我转到 /password_resets/new 时,填写表单并提交,创建方法被正确调用,因为当 /password_resets 发生 POST 时会调用 PasswordResetsController::create() 方法。
但是,当我将表单放入另一个视图(例如 /app/views/test/index.html.erb)时,填写表单并提交,我得到
未知操作
没有任何操作响应索引。行动: access_forbidden,admin_created?, 检查角色、创建、编辑、查找订单、 包括、新的、role_requirements、 角色要求=、标题、标题=、以及 更新
为什么移植表单不起作用有什么想法吗?
I have the following form in my /app/views/password_resets/new.html.erb view
<% form_tag password_resets_path do %>
<label><%= t(:email) %>:</label><br />
<%= text_field_tag "email" %><br />
<br />
<%= submit_tag t("reset_password") %>
<% end %>
along with a controller called PasswordResetsController containing a create method:
def create
@user = User.find_by_email(params[:email])
if @user
@user.deliver_password_reset_instructions!
self.notice = t("password_reset_instructions_are_mailed")
redirect_to root_url
else
flash[:error] = t("no_user_found")
render :action => :new
end
end
When I go to /password_resets/new, fill out the form, and submit, the create method is invoked properly, since the PasswordResetsController::create() method is invoked when a POST happens to /password_resets.
However, when I put the form in another view, say, /app/views/test/index.html.erb, fill out the form, an submit, I get
Unknown action
No action responded to index. Actions:
access_forbidden, admin_created?,
check_roles, create, edit, find_order,
included, new, role_requirements,
role_requirements=, title, title=, and
update
Any ideas why transplanting the form is not working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明该问题与 SSL 有关。这显示在日志中:
我的页面有需要 SSL 的表单,Rails 不喜欢我将表单从 SSL 页面提交到非 SSL 页面。因此,作为一种解决方法,由于我使用的是 ssl_requirement,所以我只是放入了
password_resets 控制器,现在一切正常了。
Turns out the issue was related to SSL. This showed up in the log:
The page I had the form on required SSL, and Rails did not like me submitting the form from an SSL page to to a non-SSL one. So, as a workaround, since I'm using ssl_requirement, I just put
in the password_resets controller, and now things work.