5

我已经尝试过不同的 make flash[:notice] 变体无需重新加载即可工作。

Stackoverflow 给了我这个 -你如何使用 Ajax 请求处理 Rail 的闪存?,但我找不到解决方案,这对我有用。

例如,添加到我的控制器:

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();

但它没有用。

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

4

2 回答 2

2

你的 js 模板文件名有错字吗?它应该是 create.js.erb 而不是 create.js
,请严格遵循https://stackoverflow.com/a/8873592/557863,然后对其进行更改。

于 2012-12-05T03:44:08.227 回答
0

You're submitting new entries via Ajax, so you shouldn't use the flash -- it's intended for redirect responses, but you're not redirecting. To indicate success or failure, you'd need to write that into your JavaScript response.

于 2014-07-17T03:00:18.733 回答