How can I implement cookieless flash messages on Rails?

16th lug 2012Ruby on Rails, , ,

I finally found a workaround implementing my own (simple) flash messages and passing them through the params from one request to another.

First of all, I overwritten default_url_options in application_controller.rb to append to every request a :my_flash param:

<code>def default_url_options   { :my_flash =&gt; @my_flash } end     </code>

Then, always in application_controller.rb, I wrote a my_flash_from_params before_filter to set the @my_flash variable:

<code>def my_flash_from_params   @my_flash = params[:fb_flash] end </code>

Finally I rendered the following _my_flash.html.erb partial in application.html.erb

<code>&lt;div class="my_flash"&gt;   &lt;%= my_flash %&gt; &lt;/div&gt;   </code>

Calling:
 <%= render :partial => "layouts/my_flash", :locals => {:my_flash => @my_flash} if @my_flash %>

If you want to try this solution see also this answer about default_url_options rewriting.

Source: Stackoverflow