I have a form used for creating a team (the model is Team).
The form adds members to the team (via the model TeamMember).
Each team works for a company. All users belong to a company.
I currently have a validation on the TeamMember model which prevents a user from becoming a team member when the user works for a different company than the team works for.
Recently, I’ve been asked to modify this functionality, since there are occasions when a team needs to add a member who works for a different company.
My simple validation has now become complicated.
I now need to provide a warning when a proposed team member works for a different company, and give the user a chance to confirm (yes/no) whether this is ok before saving the change.
What is a clean, rails-friendly way to accomplish this? I imagine there is an Ajax solution (maybe a lightbox), but I’m not sure the best way to implement it or whether someone already has a clever Gem/Plugin to handle this situation.
I would use a custom validation for this, as you are now, and add a modifiable attribute to the model that doesn’t save to the db. For example:
What you need to do is add a check in your validation method if self.user_confirmed is false. If it is, in addition to the db relation you described above, make it invalid. Then on your view, add an if statement:
Keep in mind since check_box method uses strings, youll need to convert the result of that attribute in your params hash to a boolean.
I think that should do it. Note that you may need to adjust the if statement if any other validation errors might be added to the same key.