1

我有一个非常简单的表格

  span data-controller='reassignment'
    = simple_form_for some_model, remote: true, html: {'data-target': 'reassignment.form'} do |f|
      = f.association :user,
        collection: User.all,
        label: false,
        include_blank: 'Unassigned',
        input_html: { \
          "data-target": "reassignment.user", \
          "data-action": "change->reassignment#submitForm" \
          }

现在我的刺激看起来像

import { Controller } from 'stimulus'

export default class extends Controller {

  static targets = ['user', 'form']

  submitForm(){
    this.formTarget.submit()
  }

  onPostSuccess(event) {
    console.log("success!")
  }
}

表单提交但它不尊重remote: true.

4

2 回答 2

1

这应该可以解决问题。

import { Controller } from 'stimulus'
import Rails from "@rails/ujs"

export default class extends Controller {

  static targets = ['user', 'form']

  submitForm(){
    Rails.fire(this.formTarget, 'submit')
  }

  onPostSuccess(event) {
    console.log("success!")
  }
}
于 2021-02-27T06:14:58.363 回答
0

尝试在提交时使用 preventDefault。您需要阻止按钮执行正常操作。

submitForm(e){
  e.preventDefault()
  this.formTarget.submit()
}
于 2021-01-11T16:16:46.697 回答