flash[:notice] / flash[:error]

So, I was using something like this in my controllers:

if @user.update_attributes(params[:user])
  flash[:notice] = "Profile updated"
  redirect_to :action => 'edit'
else
  flash[:error] = @user.errors.full_messages.first
  render :action => 'edit'
end

This worked fine, but if a flash[:notice] or flash[:error] was generated and I then clicked on another page that also displayed these flash messages, the new page would display the previous page’s flash messages. Not nice. A quick search returned this site, which recommended to change the code to:

if @user.update_attributes(params[:user])
  flash[:notice] = "Profile updated"
  redirect_to :action => 'edit'
else
  flash[:error] = @user.errors.full_messages.first
  redirect_to :action => 'edit'
end

Using redirect_to instead of render worked a charm. Yeehaw!

Leave a Reply

You must be logged in to post a comment.