ruby 1.9.3和rails 3.2.2中的flash消息问题

时间:2023-01-18 09:26:49

I have a issue with flash message in my application. Actually in my application i have used the devise for users authentication and my application with ruby 1.9.3 and rails 3.2.2.

我的应用程序中有flash消息的问题。实际上在我的应用程序中,我使用了用于用户身份验证的设计和我的应用程序与ruby 1.9.3和rails 3.2.2。

When an user is login, logout and sign up for new account the devise flash[:notice] is working fine.

当用户登录,注销并注册新帐户时,设计闪存[:通知]工作正常。

In Rails flash[:notice] and flash[:alert] are the default flash messages.

在Rails中闪烁[:notice]和flash [:alert]是默认的闪存消息。

The flash messages are display only once when page reload or when the user negative from one page to other page

当页面重新加载或用户从一个页面到另一个页面为负时,闪烁消息仅显示一次

The issue is when user is login the devise flash[:notice] is displaying but when i reload the page the flash[:notice] is displaying, but in rails the flash[:notice] will display only once

问题是当用户登录设备闪光灯[:通知]正在显示但是当我重新加载页面时闪光灯[:通知]正在显示,但在导轨中闪光灯[:通知]只会显示一次

Actually the issue is when i try to create a new post i have redirect to the show page and i have write helper method for flash message this method i have call from the application layout for displaying the flash messages.

实际上问题是当我尝试创建一个新帖子时,我已经重定向到显示页面,我已经为flash消息编写了帮助方法,我从应用程序布局调用了这个方法来显示flash消息。

In controller create method

在控制器创建方法中

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

The Helper method for displaying flash messages

用于显示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? ? "" : "<div class=\"#{type}\"><p>#{flash[type]}</p>     </div>"
  end
  html.html_safe
end

i have call this method form the application layout

我从应用程序布局调用此方法

= display_flash

I have tried with the flash[:alert], flash[:error],flash[:message] but no message display on the view page and i have tried with gem called flash_message this also displays only the flash[:notice]

我尝试过使用flash [:alert],flash [:error],flash [:message]但是在查看页面上没有显示消息,我尝试使用名为flash_message的gem,这也只显示flash [:notice]

Please help me to solution this issue

请帮我解决这个问题

1 个解决方案

#1


-1  

Hy i am using using this approach to show flash message.First i make partial

Hy我正在使用这种方法来显示flash消息。首先我做了部分

_flash.html.erb in shared.The code of this partial

_flash.html.erb in shared。这个部分的代码

 <% [:alert, :notice, :error].select { |type| !flash[type].blank? }.each do |type| %>
<p>
  <% if flash[:notice] %>
    <div class="alert-message error">
      <h2 style="color: #ffffff;">Notice:</h2> <br/>
      <%= flash[type] %>
    </div>
<% elsif flash[:error] %>
    <div class="alert-message error">
      <h2 style="color: #ffffff;">Errors</h2> <br/>
      <% flash[:error].each_with_index do |error, index| %>
          <%= index+1 %>. <%= error %> <br/>
      <% end %>
    </div>
  <% end %>


   </p>
  <% end %>

and i call it in application layout like this

我在这样的应用程序布局中调用它

  <div id="flash">
    <%= render :partial => 'shared/flash', :object => flash %>
  </div>

And in controller use notice,alert like this

在控制器使用通知中,这样提醒

  flash[:notice] = 'Admin was successfully created.'
  flash[:alert] = 'Admin was successfully created.'

But for showing errors i use array because it may be more than one.Like this

但是为了显示错误,我使用数组,因为它可能不止一个。就像这样

       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

end

add this line in application.js

在application.js中添加此行

   setTimeout("$('#flash').html(' ');", 10000);

Use it and enjoy!!!!!!!!!!!!!!

使用它并享受!!!!!!!!!!!!!!

#1


-1  

Hy i am using using this approach to show flash message.First i make partial

Hy我正在使用这种方法来显示flash消息。首先我做了部分

_flash.html.erb in shared.The code of this partial

_flash.html.erb in shared。这个部分的代码

 <% [:alert, :notice, :error].select { |type| !flash[type].blank? }.each do |type| %>
<p>
  <% if flash[:notice] %>
    <div class="alert-message error">
      <h2 style="color: #ffffff;">Notice:</h2> <br/>
      <%= flash[type] %>
    </div>
<% elsif flash[:error] %>
    <div class="alert-message error">
      <h2 style="color: #ffffff;">Errors</h2> <br/>
      <% flash[:error].each_with_index do |error, index| %>
          <%= index+1 %>. <%= error %> <br/>
      <% end %>
    </div>
  <% end %>


   </p>
  <% end %>

and i call it in application layout like this

我在这样的应用程序布局中调用它

  <div id="flash">
    <%= render :partial => 'shared/flash', :object => flash %>
  </div>

And in controller use notice,alert like this

在控制器使用通知中,这样提醒

  flash[:notice] = 'Admin was successfully created.'
  flash[:alert] = 'Admin was successfully created.'

But for showing errors i use array because it may be more than one.Like this

但是为了显示错误,我使用数组,因为它可能不止一个。就像这样

       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

end

add this line in application.js

在application.js中添加此行

   setTimeout("$('#flash').html(' ');", 10000);

Use it and enjoy!!!!!!!!!!!!!!

使用它并享受!!!!!!!!!!!!!!