mcoomey
mcoomey

Reputation: 121

Is there a simpler way to add user confirmation to a first_or_create method in Rails?

I have a User model that accepts nested attributes for groups. If the user attempts to join a group that doesn't exist I want to first confirm that the new group is really desired (i.e to protect against typos) and then either create the group or re-render the new action.

Currently I am using first_or_initialize to check the existence of the group and if it doesn't exist I re-render the form with a @new_group = true flag that causes the view to display a modal dialog to confirm the group creation with a 'yes' or 'no' response. The create action then looks for a params["commit"] of yes or no and acts accordingly.

This is working, but it is really clunky especially since I have to pass many other parameters through the dialog as hidden fields.

Is there a better way to add confirmation to the first_or_create / first_or_initialize method?

Upvotes: 1

Views: 40

Answers (1)

dinomix
dinomix

Reputation: 976

You could check if the group exists via ajax and then confirm on the client side, then you would only be inserting once it is already confirmed and that would be a bit more straightforward.

Upvotes: 1

Related Questions