It has been a while since I last posted a blog on rails. I have been tied up with lots of stuff lately. And I just got my time back to move on with my first Rails project to build QuoteJetty. The ride has been fun, though many times I still have to google to find answers for issues that are not covered in Rails books.

One thing that I bumped into is the usage of flash[:notice].

I found out that flash[:notice] is meant only for redirect action, because the message is only cleared after at the end of redirected view request. So if you don’t redirect the request, and you click on the next request, that message will still be displayed.

But sometimes, we just want to display custom error messages only for current request without the redirection (esp. for non-ActiveRecord error messages). So the option is to use flash.now[:notice]. It clears the flash message at the end of current request (without redirection), e.g.

def some_method
    .....
    flash.now[:notice] = 'some message'
end

But there is an issue with functional testing using flash.now[:notice]. Since the message is cleared at the end of the request, you won’t be able to retrieve the flash[:notice] value in your functional test. But no worries, here is the alternative check, use assert_tag.

assert_tag compares the actual html output of your test in a convenient way, just like most of Ruby methods. Click here for the API doc.

def test_method
    ....
    ....
    assert_tag :tag => 'div', 
        :child => /[replace with your message]/
end

8 Comments . Comments Feed . Trackback URI
Tue, 27 Mar 07 08:07 am . Chris wrote:

Thanks, this was helpful.

Tue, 27 Mar 07 09:35 am . Herryanto Siatono wrote:

Chris, you are most welcome. :)

Thu, 12 Apr 07 06:16 pm . Tiago Faro wrote:

Thanks Chris this solved my issue.

Fri, 13 Apr 07 06:00 am . justin wrote:

Thanks! I\’ve been looking around for a solution to this problem. You saved the day!

Thu, 30 Aug 07 05:46 pm . Arun wrote:

Thanks Dude nice post

Sat, 23 Feb 08 02:28 am . Manuel wrote:

Awesome. That cleared things up. Great article.

Thu, 1 May 08 10:39 pm . physio wrote:

I was hoping this would solve my problem but now I get an error that states:

‘Missing template users/some_method’ (no, I doesn’t actually say ’some_method)

Is it still trying to redirect? Do I need to prevent this somehow?

I’m getting to some_method via a link_to_remote like this:
link_to_remote ‘my link’, :url => { :action => “some_method”, …

Wed, 28 May 08 12:25 am . Sergey wrote:

Thank you Herryanto, I had this issue and you saved me some time

Add Your Comment



(optional)