When my Rails uniqueness validation fails, I would like to display the duplicate record using a partial in the form view when the user is sent back.
How do I accomplish this? Right now I only know how to alter the error message that is displayed.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There isn’t a built-in way to do this in Rails, and there’s probably a dozen ways to do it. Here’s one approach, while not elegant, should get you what you want.
1) Check if there’s a uniqueness validation error. Unfortunately there isn’t an easy way to do this, so you’ll have to check the name of the error in your controller. If there’s an error, search for the existing record, and store it in an instance variable:
2) Render the existing record in a partial in your view