ruby-on-rails - 闪光[:notice] not working when using AJAX in Rails

标签 ruby-on-rails ajax notice

我尝试了不同的 make flash[:notice] 变体,无需重新加载。
Stackoverflow 给了我这个 - How do you handle Rail's flash with Ajax requests? ,但我找不到对我有用的解决方案。
例如,添加到我的 Controller 中:

def create
  @entry = Entry.new(params[:entry])
  respond_to do |format|
  if @entry.save
    format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
    format.js {
  flash.now[:notice] = 'Website was successfully created.'
 render action: 'create'
}
  else
    format.html { render action: "new" }
    format.js { render action: "new" }
  end
  end
end

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

但它没有用。

有人可以告诉我可以理解的完整解决方案,这对他有用吗?

最佳答案

你在 js 模板文件名上打错字了吗?它应该是 create.js.erb 但不是 create.js
并请严格遵守https://stackoverflow.com/a/8873592/557863 ,然后对其进行更改。

关于ruby-on-rails - 闪光[:notice] not working when using AJAX in Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11758855/

相关文章:

jquery - 加载 div 后,jquery ui 对话框不会再次打开

javascript - 使用ajax发送base64图像

wordpress - 是否所有 Wordpress 网站都需要欧盟 Cookie 通知?

ruby-on-rails - 使用 Virtus 的 Rails 表单对象 : has_many association

jquery - 当 DOM 更改时,Capybara Webkit 未检测到 CSS 更新

ruby-on-rails - 在 index.html.erb View 中显示回形针图像

ruby-on-rails - 两个 docker 容器如何相互通信?

JQuery Mobile ajaxEnabled + asp.net 表单 postbackurl = issues

postgresql - 减少 plpgsql 中烦人的通知

php - PHP循环抛出通知,这似乎是一个持续的错误