我想在普通老式 HTTP 用户和我能够设计的 UJS 用户之间更清晰地共享 FlashHash 通知。请允许我先向您展示我拥有的东西:
首先,respond_to
的一部分相关控制人的部分#update
:
respond_to do |format|
if @cont.save
flash[:notice] = 'Continent was successfully updated.'
format.html { redirect_to edit_continent_url(@cont) }
format.json { head :ok }
format.js
和一个update.js.erb
匹配它:
$('#jsflash').html('<%= escape_javascript(render :partial => "application/notices/notice") %>').show();
<% flash.clear %> # clear the flash to avoid displaying on page reloads
同样在我的application.html.erb
有
<div id="jsflash"></div>
<%= render "application/notice/alert" %>
和app/views/application/notices/_notice.html.erb
:
<% unless flash[:notice].blank? %>
<div class="alert-message info fade in" data-alert="alert">
<a class="close" href="#">×</a>
<%= content_tag :p, flash[:notice] %>
</div>
<% end %>
我不喜欢的事情:
- 也许它是肛门的,但我宁愿不渲染
<div id="jsflash"></div>
除非我绝对必须这样做。 <% flash.clear %>
在更新 js 中存在以在页面重新加载时清除闪出。然而,这将变得非常乏味且容易出错,因为每个 JS View 都需要有这个但可能会忘记包含它。<% flash.clear %>
,同样,这是 Angular 色的混淆, View 驱动应用程序状态。- 看起来应该已经有一些工具可以做到这一点。我是否忽略了什么?
最佳答案
- 避免渲染
<div id="jsflash"></div>
当不需要它时,您将希望将 flash block 包装在 if 语句中。你可以把这样的东西放在你的Application.rb
中
.
<% if flash.present? %>
//Call your Render Flash Here
<% end %>
2.3.4。我从来没有用过 flash.clear
在我制作的数十个 Flash 应用程序中。您的 Flash 通话似乎也有一些不必要的重复。
关于javascript - 在 format.html 和 format.js 之间显示 Rails3 Flash 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8860686/