Rails 表单不在 POST 上调用创建控制器方法

发布于 2024-10-19 02:20:53 字数 1090 浏览 5 评论 0原文

我的 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-10-26 02:20:53

事实证明该问题与 SSL 有关。这显示在日志中:

过滤器链因 [:ensure_proper_protocol] 渲染或重定向而停止。

我的页面有需要 SSL 的表单,Rails 不喜欢我将表单从 SSL 页面提交到非 SSL 页面。因此,作为一种解决方法,由于我使用的是 ssl_requirement,所以我只是放入了

ssl_required :all

password_resets 控制器,现在一切正常了。

Turns out the issue was related to SSL. This showed up in the log:

Filter chain halted as [:ensure_proper_protocol] rendered_or_redirected.

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

ssl_required :all

in the password_resets controller, and now things work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文