【问题标题】:Rendering form in slim unexpectedly submits dataslim中渲染表单意外提交数据
【发布时间】:2016-02-23 00:38:35
【问题描述】:

我正在尝试在users/new.html.slim 中呈现多个表单。我在使用 form1 中的“下一步”按钮时遇到问题,当我单击它时,它会自动将表单提交给 db。但是,它应该只是渲染下一个表单。

= simple_form_for @user, html: { class: "profile_form"} do |f|
  = f.fields_for :profile, @user.profile || Profile.new do |p|
    = render 'users/form1', f: f, p: p
  = f.submit

这是form1

.form-1
  .form-1-detail
    = f.input :name
    = f.input :email
    = f.input :password, required: true
    = f.input :password_confirmation, required: true
button.next next

有人知道为什么会这样吗?我怀疑缩进会导致它的行为怪异。

【问题讨论】:

  • 我添加了 e.preventDefault();到我的 js.file。它看起来像工作,但不确定它是否是正确的方法。 $('.next').on('click', function (e) { e.preventDefault(); });

标签: ruby-on-rails forms slim-lang


【解决方案1】:

您的问题在于按钮的默认类型,即“提交”,因此表单内的任何按钮都会触发表单提交,除非您专门更改了按钮类型。

你可以通过显式添加type="button"来做到这一点:

.form-1
  .form-1-detail
    = f.input :name
    = f.input :email
    = f.input :password, required: true
    = f.input :password_confirmation, required: true
button.next type="button" next

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    相关资源
    最近更新 更多