ruby 1.9.3和rails 3.2.2中的flash消息问题
我的应用程序中有flash消息的问题。 实际上在我的应用程序中,我使用了用于用户身份validation的设计和我的应用程序与ruby 1.9.3和rails 3.2.2。
当用户登录,注销并注册新帐户时,设计闪存[:通知]工作正常。
在Rails中闪烁[:notice]和flash [:alert]是默认的闪存消息。
当页面重新加载或用户从一个页面到另一个页面为负时,闪烁消息仅显示一次
问题是当用户登录设备闪光灯[:通知]正在显示但是当我重新加载页面时闪光灯[:通知]正在显示,但在导轨中闪光灯[:通知]只会显示一次
实际上问题是当我尝试创建一个新post时,我已经重定向到显示页面,我已经为flash消息编写了帮助方法,我从应用程序布局调用了这个方法来显示flash消息。
在控制器创建方法中
def create @asset = Asset.new(params[:asset]) @asset.user_id = current_user.id respond_to do |format| if @asset.save format.html { redirect_to @asset, alert: 'Asset was successfully created.' } format.json { render json: @asset, status: :created, location: @asset } else format.html { render action: "new" } format.json { render json: @asset.errors, status: :unprocessable_entity } end end end
用于显示flash消息的Helper方法
FLASH_TYPES = [:error, :warning, :success, :message,:notice,:alert] def display_flash(type = nil) html = "" if type.nil? FLASH_TYPES.each { |name| html << display_flash(name) } else return flash[type].blank? ? "" : "#{flash[type]}
" end html.html_safe end
我从应用程序布局调用此方法
= display_flash
我尝试过使用flash [:alert],flash [:error],flash [:message]但是在查看页面上没有显示消息,我尝试使用名为flash_message的gem,这也只显示flash [:notice]
请帮我解决这个问题
Hy我正在使用这种方法来显示flash消息。首先我做了部分
_flash.html.erb in shared。这个部分的代码
<% [:alert, :notice, :error].select { |type| !flash[type].blank? }.each do |type| %> <% if flash[:notice] %>
<% elsif flash[:error] %> <% end %> <% end %>
我在这样的应用程序布局中调用它
<%= render :partial => 'shared/flash', :object => flash %>
在控制器使用通知中,这样提醒
flash[:notice] = 'Admin was successfully created.' flash[:alert] = 'Admin was successfully created.'
但是为了显示错误,我使用数组,因为它可能不止一个。就像这样
def create @user = User.new(params[:user]) @user.is_activated = true # @user.skip_confirmation! if @user.save role = Role.find_by_name("admin") RoleUser.create!(:user => @user, :role => role) redirect_to :controller => '/administrator', :action => 'new' flash[:notice] = 'Admin was successfully created.' else flash[:error]=[] @user.errors.full_messages.each do |error| flash[:error] << error end render :action => "new" end
结束
在application.js中添加此行
setTimeout("$('#flash').html(' ');", 10000);
使用它并享受!!!!!!!!!!!!!!