New tip Tips Rss
The Tips area is for posting useful tips or ideas about Ruby
Disable the output of IRB (replies: 0)
in irb by jtoy on Wed May 03 14:10:00 -0500 2006
last post by jtoy on Wed May 03 14:10:00 -0500 2006

Sometimes when you are testing functions, you just want to run the code without irb printing out tons of data to the console.
Normally you type in irb:
1+1
and it will print out
=> 2
if you add a ”;0” or ”;nil”
the result will not be printed, only a 0 or nil. This is only useful when a function prints out to much data to the screen such as an array with 1,000,000 items.
so you would do:
my_function;nil


show
use page caching and still have acccess to flash (replies: 1)
in rails by jtoy on Mon Apr 17 11:29:03 -0500 2006
last post by longdcr on Tue Apr 18 23:09:56 -0500 2006

If you want to use page caching, but can’t because you will loose the ability to display error and succcess messages, there is a way around this.
You can put your error in javascript so your pages are always cached, only your javascript will change.
You will need to make sure javascript is loaded from the webserver and that the browser isn’t caching your javascript.
In your javascript template have some code like:

<% if flash[:js_notice]  %>

  var js_notice = "<%= flash[:js_notice]  %>"
<% else %>

  var js_notice = null
<% end %>


<% if flash[:js_error]  %>

  var js_error = "<%= flash[:js_error] %>"
<% else %>


function display_flash(){
  if (js_notice){
    var div = document.getElementById("js_notice")
    div.innerHTML =  js_notice
  }
  if (js_error){
    var div = document.getElementById("js_error")
    div.innerHTML = js_error

  }
}
window.onload = display_flash

Then in your layout have this html:
  <div id="js_error"></div>
  <div id="js_notice"></div>

On each new page request, the javascript code will load and check if there are notices or errrors, if there are, then it will place that error on your page in the appropiate div.
show