【问题标题】:Why am I getting a JQuery alert twice in Rails?为什么我在 Rails 中两次收到 JQuery 警报?
【发布时间】:2018-01-05 12:37:49
【问题描述】:

我在删除链接上有一个 jquery 确认。单击链接时会弹出确认信息,但是单击“确定”时会再次弹出确认信息。我不知道为什么。除了必须单击“确定”两次之外,该方法工作正常并且帖子被删除。


show.html.haml

%h1= @post.title
%p= @post.description

%p= link_to 'Back', root_path
%p= link_to 'Edit', edit_post_path
%p= link_to 'Delete', @post, method: :delete, data: { confirm: 'Are you sure you want to delete this Post?' }

posts_controller.rb

class PostsController < ApplicationController
  before_action :find_post, only: %i[show edit update destroy]
.
.
.
  def destroy
      @post.destroy
      redirect_to root_path, notice: 'Post successfully deleted.'
  end
.
.
.
  def find_post
      @post = Post.find(params[:id])
  end
end

Gemfile

gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'coffee-rails', '~> 4.2'
gem 'haml', '~> 5.0', '>= 5.0.4'
gem 'jbuilder', '~> 2.5'
gem 'jquery-rails', '~> 4.3', '>= 4.3.1'
gem 'puma', '~> 3.7'
gem 'rails', '~> 5.1.4'
gem 'sass-rails', '~> 5.0'
gem 'simple_form', '~> 3.5'
gem 'sqlite3'
gem 'turbolinks', '~> 5'
gem 'uglifier', '>= 1.3.0'

application.js

//= require jquery
//= require jquery_ujs
//= require rails-ujs
//= require turbolinks
//= require_tree .

【问题讨论】:

  • 这可能在您的 jquery 代码中?检查它或在此处发布以寻求帮助
  • jquery 代码是gem 插入管道的标准jquery。我没有以任何方式更改源 jquery。不过,我会检查 jquery gems github 上的任何错误报告。谢谢。
  • @olucube,你是在哪里发表评论然后删除它询问是否需要 jquery_ujs 和 rails-ujs?
  • 没有得到您的问题
  • 有人发表评论询问我是否需要 application.js 文件中的 jquery_ujs 和 rails-ujs。他们在我回复之前删除了评论。我在问你是不是发表评论的人。

标签: jquery ruby-on-rails


【解决方案1】:

从你的 application.js 中删除 jquery_ujs,因为 rails_ujs 对以后的 rails 版本已经足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多