ruby 1.9.3 和 rails 3.2.2 中的 flash 消息问题 [英] Issue with flash messages in ruby 1.9.3 and rails 3.2.2

查看:25
本文介绍了ruby 1.9.3 和 rails 3.2.2 中的 flash 消息问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的 Flash 消息有问题.实际上,在我的应用程序中,我使用了该设备进行用户身份验证,并将我的应用程序与 ruby​​ 1.9.3 和 rails 3.2.2 一起使用.

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

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

当页面重新加载或用户否定从一个页面到另一页面时,Flash 消息仅显示一次

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

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

在控制器中创建方法

def 创建@asset = Asset.new(params[:asset])@asset.user_id = current_user.idresponse_to do |格式|如果@asset.saveformat.html { redirect_to @asset, alert: '资产已成功创建.'}format.json { 渲染 json: @asset, 状态: :created, location: @asset }别的format.html { 渲染动作:新"}format.json { 渲染 json: @asset.errors, 状态: :unprocessable_entity }结尾结尾结尾

显示Flash消息的Helper方法

FLASH_TYPES = [:error, :warning, :success, :message,:notice,:alert]def display_flash(type = nil)html = ""如果type.nil?FLASH_TYPES.each { |名称|html <<display_flash(名称)}别的返回 flash[type].blank??"" : "<div class=\"#{type}\"><p>#{flash[type]}</p> </div>"结尾html.html_safe结尾

我在应用程序布局中调用了这个方法

= display_flash

我尝试过使用 flash[:alert]、flash[:error]、flash[:message] 但在视图页面上没有显示消息,我尝试使用名为 flash_message 的 gem 这也只显示闪光[:通知]

请帮我解决这个问题

解决方案

我正在使用这种方法来显示 Flash 消息.首先我制作部分

_flash.html.erb 在shared.this部分的代码

 <% [:alert, :notice, :error].select { |type|!flash[type].blank?}.每个都做|类型|%><p><% if flash[:notice] %><div class="alert-message error"><h2 style="color: #ffffff;">注意:</h2><br/><%=闪光[类型]%>

<% elsif flash[:error] %><div class="alert-message error"><h2 style="color: #ffffff;">错误</h2><br/><% flash[:error].each_with_index do |error, index|%><%=指数+1%>.<%=错误%><br/><%结束%>

<%结束%></p><%结束%>

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

 

<%=渲染:部分=>'共享/闪存', :object =>闪点%>

在控制器中使用通知,这样的警报

 flash[:notice] = '管理员已成功创建.'flash[:alert] = '管理员已成功创建.'

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

 def 创建@user = User.new(params[:user])@user.is_activated = true# @user.skip_confirmation!如果@user.saverole = Role.find_by_name("admin")RoleUser.create!(:user => @user, :role => role)重定向到:控制器 =>'/管理员', :action =>'新的'flash[:notice] = '管理员已成功创建.'别的闪光[:错误]=[]@user.errors.full_messages.each 做 |error|闪光[:错误] <<错误结尾渲染:动作=>新的"结尾

结束

在 application.js 中添加这一行

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

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

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.

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.

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.

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_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]

Please help me to solution this issue

解决方案

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

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

 <% [: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

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

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

这篇关于ruby 1.9.3 和 rails 3.2.2 中的 flash 消息问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆