Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ruby 1.9.3和rails 3.2.2中的flash消息问题_Ruby On Rails - Fatal编程技术网

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

Ruby on rails ruby 1.9.3和rails 3.2.2中的flash消息问题,ruby-on-rails,Ruby On Rails,我的应用程序中存在flash消息问题。实际上,在我的应用程序中,我使用了Desive for users authentication,我的应用程序使用了ruby 1.9.3和rails 3.2.2 当用户登录、注销并注册新帐户时,设备flash[:notice]工作正常 在Rails中,flash[:notice]和flash[:alert]是默认的flash消息 页面重新加载或用户从一页转到另一页时,闪存消息仅显示一次 问题是,当用户登录时,显示的是设备flash[:notice],但当我重

我的应用程序中存在flash消息问题。实际上,在我的应用程序中,我使用了Desive for users authentication,我的应用程序使用了ruby 1.9.3和rails 3.2.2

当用户登录、注销并注册新帐户时,设备flash[:notice]工作正常

在Rails中,flash[:notice]和flash[:alert]是默认的flash消息

页面重新加载或用户从一页转到另一页时,闪存消息仅显示一次

问题是,当用户登录时,显示的是设备flash[:notice],但当我重新加载页面时,flash[:notice]会显示,但在rails中,flash[:notice]只会显示一次

事实上,问题是当我试图创建一个新的帖子时,我已经重定向到显示页面,并且我已经为flash消息编写了write helper方法。这个方法我已经从应用程序布局中调用,用于显示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? ? "" : "<div class=\"#{type}\"><p>#{flash[type]}</p>     </div>"
  end
  html.html_safe
end
我试过使用flash[:alert]、flash[:error]、flash[:message],但在查看页面上没有显示消息,我试过使用名为flash_message的gem,它也只显示 闪光[注意]


请帮助我解决此问题

为什么我使用此方法来显示flash消息。首先,我做了部分修改

_shared中的flash.html.erb。此部分的代码

 <% [: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 %>
但为了显示错误,我使用数组,因为它可能不止一个

       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
使用它并享受它

  <div id="flash">
    <%= render :partial => 'shared/flash', :object => flash %>
  </div>
  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
   setTimeout("$('#flash').html(' ');", 10000);