ruby on rails - Display flash notices in various locations -
i have temp;late menu down lhs has search function restrict menu options.
some times user may search option returns no results.
when search returns no results display flash notice above search box communicate this.
in addition menu flash use flash communicate normal messages associated form operations in main part of view such record save, user logged in etc.
is possible display multiple flash messages on 1 view or there better way achieve this?
i had resolved same scenario using flash ajax can use id or class selector update html in place in app.
show flash using ajax add in application_helper
def flash_display response = "" flash.each |name, msg| msg=msg response = response + content_tag(:div, msg, :id => "flash_#{name}",:class=>"alert alert-danger") "#{msg}".html_safe end end flash.discard response end
create js file show flash messages using id/class selector such .....ajax_flash_error.js.erb
<% if type== "user" %> $('#flash_user').html("<%= escape_javascript raw(flash_display) %>"); <% else %> $('#common_flash').html("<%= escape_javascript raw(flash_display) %>"); <%end%>
send flash message controllers in usual manner such users_controller.rb
def create if user //do comething else respond_to |format| flash.now[:error]="you cannot edit image" format.js { render 'shared/ajax_flash_error',:locals=>{:type=>"user"}} end end end
show flash message in layout or view file.... application.html.erb
<html> <head></head> <body> <div id="common_flash"> <% flash.each |name, msg| %> <%= msg%> <%end%> </div> ####some other view file,use other flash id <div id="flash_user"> <% flash.each |name, msg| %> <%= msg%> <%end%> </div> </body> </html>
in way can use multiple divs unique id/class , show flash messages wherever want throughout application
...hope helps**
Comments
Post a Comment