What is the best way to push error messages on redirect to?
I’ve previously used couple of approaches, but both of them has issue.
(1) Passing the entire object with error on flash and using error_messages_for:
def destroy
if @item.destroy
flash[:error_item] = @item
end
redirect_to some_other_controller_path
end
I found that this method causes cookie overflows.
(2) Passing a single error message:
def destroy
if @item.destroy
flash[:error] = @item.full_messages[0]
end
redirect_to some_other_controller_path
end
This way I only send a single error message, what if there are many?
Does Anyone knows a better way?
Firstly,
you can achieve what you’re trying to do by setting a single sentence.
I think you could also set it as the array without overflowing the cookie.
But as the other guys say, flash is generally better off being used to return specific messages.
eg.
A common pattern is as such.
Namely, we have two paths.
@record.errorson hand to callerror_messages_for(@record)if we so wish.