Reputation: 13
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
Upvotes: 1
Views: 2198
Reputation: 10885
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!!!!!!!!!!!!!!
Upvotes: -1