我有一张像这样提交的表格:
class PreferenceForm
constructor: (preference_form) ->
@preference_form = preference_form
@preview_update_button = @preference_form.find $("[data-behavior='preview-update-button']")
@submit_url = @preference_form.attr('action')
@setEvents()
setEvents: ->
@preference_form.on "submit", @handleSubmit
handleSubmit: (event) =>
event.preventDefault()
console.log @submit_url
$.ajax(
url: @submit_url,
method: "PATCH"
dataType: "JSON"
data: @preference_form.serialize()
success: @handleSuccess
)
控制台将@submit_url记录为/user_preferences/#
,这是正确的。但是,当我实际尝试提交表单时,它会将PATCH请求发送到当前页面而不是我尝试将其发送到的URL。
导致此行为发生的原因是什么?我怎样才能让它按照我的意图行事呢?
一切都像这样实例化:
class CertificatePreviewer
constructor: (certificate_previewer) ->
@certificate_previewer = certificate_previewer
@preference_form = new PreferenceForm @certificate_previewer.find $("[data-behavior='preference-form']")
jQuery ->
new CertificatePreviewer $("[data-behavior='certificate-previewer']")