How can I either work with the ruby on rails flash hash manually, or create my own "custom" hash for error messages? -


in rails 3 project, i'm trying create consistent error/success message display dialog go in header of page. have displaying flash messages when set controllers, this:

  <% if flash.length > 0 %>        <% flash.each |key, value| %>             <% if key == "error" %>                 <div class="notice error"><%= value %></div>            <% elsif key == "success" %>                 <div class="notice success"><%= value %></div>                  <% else %>                <div class="notice regular"><%= value %></div>            <% end %>        <% end %>    <% end %> 

but, want able send custom messages dialog come link_to tags, example:

<% if [certain variable set] %>      <% message = hash.new %>     <% message[type] = "specific message sent link" %>     <%= link_to [link option one] %>  <% else %>      <%= link_to [link option two] %>  <% end %> 

where "type" either "error", "success", or else.

when try accomplish manually putting message in flash hash this:

 <% flash[:error] = "specific message sent link" %>  <%= link [link option one] %> 

...the message shows when user clicks link, isn't removed after user clicks other links. message remains in flash hash despite attempts remove manually. message shows in header no matter page user goes to, if flash hash doesn't have new added it.

when try code "message" hash above, testing "message" in header , sending wherever want, hash called "message" isn't set on pages (only page send to). understandably results in error of

     undefined local variable or method 'message' 

whenever 'message' not set in page. i've tried things like

      <% if message %>            <%# show error message %>       <% end %> 

and

 <% if !message.nil? %>       etc 

but still undefined error.

is there way can either

a) manually clear specific hash value after sending manually,

b) stop "undefined variable" error occurring when 'message' undefined,

or

c) solve problem in way?

thanks much!

the message showing because setting flash[:error] when "certain variable set." flash in case has no relationship link_to whatsoever. should setting flash[:error] in controller whatever action link_to pointing to.

an abundance of logic in view (as seen above) code smell. should avoid whenever possible (in case putting flash logic in controller).


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -