This feature is schedule for: Rails v2.3 stable
Rails’ flash is a convenient way of passing objects (though mostly used for message strings) across http redirects. In fact, every time you set a flash parameter the very next step is often to perform your redirect w/ redirect_to:
1 2 3 4 5 6 7 |
class UsersController < ApplicationController def create @user = User.create(params[:user]) flash[:notice] = "The user was successfully created" redirect_to user_path(@user) end end |
I know I hate to see two lines of code where one makes sense – in this case what you’re saying is to “redirect to the new user page with the given notice message” – something that seems to make more sense as a singular command.
DHH seems to agree and has added :notice, :alert and :flash options to redirect_to to consolidate commands. :notice and :alert automatically sets the flash parameters of the same name and :flash let’s you get as specific as you want. For instance, to rewrite the above example:
1 2 3 4 5 6 |
class UsersController < ApplicationController def create @user = User.create(params[:user]) redirect_to user_path(@user), :notice =>"The user was successfully created" end end |
Or to set a non :alert/:notice flash:
1 2 3 4 5 6 |
class UsersController < ApplicationController def create @user = User.create(params[:user]) redirect_to user_path(@user), :flash => { :info => "The user was successfully created" } end end |
I’ve become accustomed to setting my flash messages in :error, :info and sometimes :notice making the choice to provide only :alert and :notice accessors fell somewhat constrained to me, but maybe I’m loopy in my choice of flash param names.
Whatever your naming scheme, enjoy the new one-line redirect!
tags: ruby, rubyonrails

The functionality of redirect_to and flash should not be combined into a single method, it’s silly. Is it that difficult to build your own helper so the rest of us don’t have to deal with bloat?
Not sure this constitutes as bloat—without looking at the source it seems like a one line addition to Rails code. What it does is demystify flash and puts it into the context of a redirect.
My question is, what does this do with flash.now? Is that going to be included in this new “bloat?”
I guess it’s a matter of convenience to have them both in a single line. I am up for it since you hardly see flash alone in a Rails controller. Other than that, you can write them separately as well.
It’s would be nice if
class UsersController < ApplicationController def create @user = User.create(params[:user]) redirect_to user_path(@user), :notice => true end end
automatically look at ‘users.notice.flash’ in I18n locales.
mrkris, you know that’s what we do in Rails, right? We spot commonalities from real applications in the wild and we extract those into reusable abstractions.
This is how this came to be. I was looking over the code in a new application and kept seeing this pattern. Then I looked at a bunch of other applications. Probably 80-90% of all flash usage in these applications followed this pattern of setting a notice or an alert just before a redirect. This embraces that pattern and makes it first class.
So if you consider abstractions like this bloat, I think you might be running with the wrong crowd. This is an archetypical definition of what we do in Rails.
DHH, will flash.now follow the same pattern for render in controller? If not, what’s the motivation?
I like this feature, it really makes sense. I’m not really embracing the naming :alert/:notice, but I as you said in the GH comments it is possible to override. I’m not sure that 90% of Rails developers use:alert/:notice though. Anyway, no big issue.
To Rails Core, Thank YOU for providing it!
I kept seeing this pattern over and over so I made my own bloated methods to support this for so many rails products I coded since 2006 only for this! Now I can start using it from its pure rails.
I don’t think adding this can be categorized as bloat, but I must admit it seems a bit odd, almost untasteful, to add this to redirect_to. But, given that I have a differing opinion on this particular issue, I’m probably running with the wrong crows ;-P
cah caw
I’ve always used flash[:error] as well. Maybe I’ll switch to :alert once this is in a stable release.
Unloppilk rdod