jquery - 无法使用 AJAX CRUD 操作发出通知以及如何在不重新加载的情况下显示通知?

标签 jquery ruby-on-rails ajax

已编辑

尝试了很多解决方案,但没有任何效果。

这是来自 create.js.erb 的代码:

$('<%= j render @website %>').appendTo('#websites').hide().fadeIn();
$("#new_website")[0].reset();
$(".alert").html("<%= escape_javascript(flash[:notice]) %>"); $(".alert").show(300); 

这是我的 Controller :

   def create
@website = current_user.websites.new params[:website]

respond_to do |format|
  if @website.save
   format.html { redirect_to  @website, notice: 'Website was successfully created.' }
  format.js {
    flash[:notice] = 'Website was successfully created.'
    render action: 'create'
  }

   else
    format.html { render action: "new" }
    format.js { render action: "new" }
   end
  end
end

我的通知警报的 HTML:

       <div class="alert alert-notice">

你能告诉我为什么它不起作用吗?

最佳答案

看来你只是在响应html请求时设置通知,而不是响应js请求,我认为你需要这样的东西:

def create
  @website1 = Website1.new(params[:website1])

  respond_to do |format|
    if @website1.save
      format.html { redirect_to @website1, notice: 'website1 was successfully created.' }
      format.js {
        flash[:notice] = 'website1 was successfully created.'
        render action: 'create'
      }
    else
      format.html { render action: "new" }
      format.js { render action: "new" }
    end
  end
end

关于jquery - 无法使用 AJAX CRUD 操作发出通知以及如何在不重新加载的情况下显示通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736108/

相关文章:

javascript - AngularJS POST 失败 : Response for preflight has invalid HTTP status code 404

javascript - AJAX 未从 php 返回变量

jquery - 用jquery显示时间

javascript - 如何使用 jQuery 和 JavaScript 获取 URL 的特定部分?

javascript - CakePHP REST API 为 AJAX 提供 403 但不为 cURL

ruby-on-rails - rails : How to restart sidekiq?

ruby-on-rails - Ruby 2.0 是否改变了 SimpleDelegator 的行为?

jquery - 将标签内容替换为错误消息

javascript - JQuery 未从 PHP echo 运行

ruby-on-rails - Rails 3.2 Nginx Unicorn 总是尝试从公共(public)文件夹加载index.html (403)