The use of :alert (or :notice) with the render method, from the Ruby On Rails guide called 'Layouts and Rendering in Rails', does not work for me:
I'm confused as to why that Rails Guide mentions using flash values in render
, since they only appear to work in redirect_to
at the moment. I think you'll find your approach works if you put a flash.now[:alert] = 'Alert message!'
before your render method call.
Edit: this is a flaw in the guides that will be fixed, you should use the separate method call to set the flash prior to calling render.
Try
def bye @counter = 4 flash.now[:error] = "Your book was not found" render :index end
Normally you would do something like:
if @user.save redirect_to users_path, :notice => "User saved"else flash[:alert] = "You haz errors!" render :action => :newend
What you want to do is (and I like this syntax much better):
if @user.save redirect_to users_path, :notice => "User saved"else render :action => :new, :alert => "You haz errors!"end
...however, that isn't valid for ActionController::Flash#render
.
But, you can extend ActionController::Flash#render
to do exactly what you want:
Create config/initializers/flash_renderer.rb
with the following content:
module ActionController module Flash def render(*args) options = args.last.is_a?(Hash) ? args.last : {} if alert = options.delete(:alert) flash[:alert] = alert end if notice = options.delete(:notice) flash[:notice] = notice end if other = options.delete(:flash) flash.update(other) end super(*args) end endend