Passing error messages through flash

Firstly,
you can achieve what you’re trying to do by setting a single sentence.

flash[:error] = @item.errors.full_messages.to_sentence

I think you could also set it as the array without overflowing the cookie.

flash[:error] = @item.errors.full_messages

But as the other guys say, flash is generally better off being used to return specific messages.

eg.

flash[:error] = "We were unable to destroy the Item"

A common pattern is as such.

def some_action
  if @record.action
    flash[:notice] = "Action performed successfully"
    redirect_to @record      
  else
    flash[:error] = "Action failed"
    render :action => "some_action"
  end
end

Namely, we have two paths.

  1. Action succeeds. We redirect.
  2. Action fails. We show a page, flash an error, and have @record.errors on hand to call error_messages_for(@record) if we so wish.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)