Rails Ajax form_for 正在启用多次提交(无法禁用提交按钮)

Rails Ajax form_for is enabling multiple submissions (cannot disable submit button)

我对用于呈现错误的 ajax 表单有疑问,但它允许用户基本上无限次提交,只要他们继续按下提交按钮即可。发生的事情是当用户按下提交按钮时,它会变灰几分之一秒并显示 disable_with 文本,然后再次变为可点击。然后用户可以继续按下并发出 post 请求。只有当用户停止时,重定向才会发生。我基本上搜索了这个特定问题的所有答案,但它们似乎对我不起作用。我想知道是否有人可以提供帮助。

我试过使用 javascript 禁用按钮并使用数据:{disable_with: "..."}。

这是我现在的 .html.erb 代码:

<div class="container-fluid main-container">
  <%= form_for [@group, @task], remote: true do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name, class: "form-control" %>

    <%= f.label :description %>
    <%= f.text_area :description, class: "form-control" %>

    <%= f.label :priority %>
    <%= f.text_field :priority, class: "form-control" %>

    <%= f.label :user %>
    <%= f.select :user_id, options_for_select(@group.options_for_user), {}, class: "form-control" %>

    <%= f.submit "Create Task", class: "btn btn-primary new-task-button", data: {disable_with: "Creating task..."} %>
  <% end %>
</div>

这是我的控制器代码,用于任务新建和创建操作

def new
  @group = Group.find(params[:group_id])
  @task = Task.new
end

def create
  new_user = User.find(task_params.delete(:user_id))
  @task = Task.new(to_update)
  respond_to do |format|
    if @task.save
      @group.tasks << @task
      new_user.tasks << @task
      format.html {redirect_to @group}
      TasksChannel.broadcast_to(@group, {action: "create", data: @task, user: new_user}) 
    else
      @errors = @task.errors
      format.js {render :file => "layouts/errors.js.erb"}
    end
  end
end

def task_params
  return params.require(:task).permit(:name, :description, :priority, :user_id)
end

我已经坚持了一段时间,这太令人沮丧了。如果有任何帮助,我将不胜感激。

  1. 检查您页面中的 html 是否具有该属性。在 localhost:3000 打开浏览器并打开开发人员工具栏以检查 html 元素。

  2. 在您的 applicationscript.js 文件中写一些 Jquery 并测试问题是否已解决。也许与 turbo-links 或其他东西有一些冲突

    $('#my-button').prop('disabled', true);
    

Disabling button after disable_with

  1. 也在 rails 上没有 ruby 的简单 jquery 项目上测试效果..