admin管理员组文章数量:1312722
I'm trying to show a flash message and render it with Ajax, but the message does not seem to be appearing until after I refresh the page.
Here's my create.rjs file:
page.insert_html :top, :feed_items, :partial => 'shared/feed_item', :object => @micropost
page.replace_html :user_info, pluralize(current_user.microposts.count, "micropost")
page[:micropost_form].reset
page.replace_html :notice, flash[:notice]
flash.discard
Here's the relevant part of my application layout view:
<div id= "notice"><% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %></div>
And here's the relevant part of my micropost controller:
class MicropostsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
respond_to do |format|
if @micropost.save
flash[:success] = "Micropost created!"
format.html { redirect_to root_path }
format.js
else
@feed_items = []
render 'pages/home'
end
end
end
So why isn't the flash message showing up right away?
I'm trying to show a flash message and render it with Ajax, but the message does not seem to be appearing until after I refresh the page.
Here's my create.rjs file:
page.insert_html :top, :feed_items, :partial => 'shared/feed_item', :object => @micropost
page.replace_html :user_info, pluralize(current_user.microposts.count, "micropost")
page[:micropost_form].reset
page.replace_html :notice, flash[:notice]
flash.discard
Here's the relevant part of my application layout view:
<div id= "notice"><% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %></div>
And here's the relevant part of my micropost controller:
class MicropostsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
respond_to do |format|
if @micropost.save
flash[:success] = "Micropost created!"
format.html { redirect_to root_path }
format.js
else
@feed_items = []
render 'pages/home'
end
end
end
So why isn't the flash message showing up right away?
Share Improve this question edited Mar 3, 2011 at 23:42 Justin Meltzer asked Mar 3, 2011 at 23:32 Justin MeltzerJustin Meltzer 13.6k34 gold badges119 silver badges182 bronze badges 2- Look at this answer for a good solution to this: stackoverflow./questions/366311/… – Peyman Commented Oct 30, 2011 at 3:44
- Not sure if it'll do exactly what you want, but try using flash.now[:success] – Kenny Bania Commented Aug 7, 2012 at 15:51
2 Answers
Reset to default 8Flash messages by design are stored in the session until the next page load. In the case of ajax, don't use flash. For one thing, the div to replace the message into may not exist, so you need to add that to your page as a container to add to. Second, just do it from a regular variable, not flash.
A regular variable would be:
@message = "Micropost created!"
instead of the flash[:success]
The js view would use this variable instead of flash.
Instead of page.replace_html :notice, flash[:notice]
Try this page.replace_html :notice, "Some info to flash"
本文标签: javascriptAjax flash message in ruby on railsStack Overflow
版权声明:本文标题:javascript - Ajax flash message in ruby on rails - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741890948a2403297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论