You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails远程表单移除data-remote后仍触发远程提交问题求助

Hey Kevin, let's break down this tricky issue you're hitting—this is a super common gotcha with Rails UJS, so I’ve got a clear explanation and fixes for you!

The Root Cause

Here's what's happening: Rails UJS (Unobtrusive JavaScript) scans your page on load and automatically binds AJAX submit events to any form with the data-remote attribute. Even if you later remove the data-remote attribute from the form, that pre-bound AJAX submit listener is still attached to the form element. The browser inspector showing the attribute is gone doesn't matter because the event was already registered when the page loaded.

Fixes to Try

Let's go through the most reliable solutions:

1. Manually Unbind the UJS Submit Event

The cleanest fix is to remove the UJS-specific submit listener after you disable data-remote. Rails UJS uses the .rails event namespace, so you can target just its bindings without affecting any custom submit handlers you've added.

Example code:

// After you've confirmed the preview and want to switch to regular submission
const form = $('#your-form-id');

// Remove the data-remote attribute
form.removeAttr('data-remote');
// Unbind the UJS submit event
form.off('submit.rails');

Once you do this, the next form submission will behave like a normal non-remote Rails form.

2. Use a Flag to Control Submission Behavior (Alternative Approach)

Instead of removing data-remote entirely, you can add a hidden field to track whether the user is previewing or confirming, then override UJS's behavior in a submit handler.

First, update your form with a hidden flag:

<%= form_with model: @your_model, remote: true, id: 'preview-form' do |f| %>
  <!-- Your form fields here -->
  <%= f.hidden_field :is_confirming, value: 'false', id: 'confirm-flag' %>
  
  <%= f.submit 'Generate Preview', id: 'preview-btn' %>
  <%= f.submit 'Confirm & Submit', id: 'confirm-btn', style: 'display: none;' %>
<% end %>

Then add this JavaScript:

// Show confirm button after preview loads
$('#preview-form').on('ajax:success', function() {
  $('#preview-btn').hide();
  $('#confirm-btn').show();
  $('#confirm-flag').val('true');
});

// Override UJS behavior when confirming
$('#preview-form').on('submit', function(e) {
  if ($('#confirm-flag').val() === 'true') {
    // Stop UJS from handling the submit
    Rails.stopEverything(e);
    // Trigger a regular form submission
    this.submit();
  }
});

This way, you don't have to worry about unbinding events—you just bypass UJS when the user is ready to submit for real.

3. Double-Check Your Attribute Removal

Make sure you're using the correct method to remove the data-remote attribute. Using form.removeAttr('data-remote') is the right call—avoid just modifying the jQuery data object with form.data('remote', false), since UJS relies on the actual HTML attribute for initial setup, but the already-bound event is the real issue here.

Quick Recap

The key takeaway is that Rails UJS binds events on page load, not dynamically as attributes change. Removing data-remote doesn't undo that initial binding, so you need to either unbind the UJS submit event or override its behavior when you're ready for a regular submission.

内容的提问来源于stack exchange,提问作者user1130176

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:49:09