Clarification on error_messages_for

Just learning Rails… So this is probably a noob question. My
Could anyone please give me a quick summary/example on the usage of

I have two tables/models, Group and GroupMembers. Obviously, a Group
has_many GroupMembers. I have a customized view which shows a list of
GroupMembers followed by a field and a button which facilitates adding a
member to the group by either email address or login.

When the user attempts to add someone to the group who is already there,
need to return to this EditMembers view with an error message.

I cannot figure out how to use error_messages_for to display the error
message when returning to the view. The most natural thing to attach
messages to seems to be the GroupMember object, but the new groupmember
object doesn’t exist until the button action and not on the first view
the groupmembers list, so I get a reference to a nil object.

If anyone could provide me with some pseudocode of how it should work,
would be great! Thanks!

Without delving specifically into your situation, remember that
ActiveRecord::new creates an object without saving it to the
database. So even if you don’t have parameters to create a new
object, you can create an empty model to avoid the nil problem.