Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:38:29+00:00 2026-06-15T09:38:29+00:00

When my Rails uniqueness validation fails, I would like to display the duplicate record

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:38:30+00:00Added an answer on June 15, 2026 at 9:38 am

    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:

    def create
      @new_model = Model.new(params[:model])
    
      if @new_model.save
        # success...
      else
        if @new_model.errors[:field].index("has already been taken")
          @existing_record = Model.where(field: model.field).first
        end
    
        render :new
      end
    end
    

    2) Render the existing record in a partial in your view

    <%= render partial: 'partial_name', locals: {existing_record: @existing_record} %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Rails 3.0.0.beta4 I want to add a validation on uniqueness on two
I would like to know how to implement a validation in Core Data. What
I was adding uniqueness validation to the model in charge of user registration. The
I have a rails model that validates uniqueness of 2 form values. If these
I'm using Rails 2.3.4/Ruby 1.9.1 for my application. In the view I have this
I have an update form in Rails 3 for admin users that fails silently,
I have a problem with the scoped uniqueness validation in Rails. It works fine
Rails has a very convenient uniqueness validation. ASP.NET MVC doesn't. I need to make
How can i display the validation error messages just below the form field rather
I'm using Clearance for authentication in my Rails application. The Clearance::User mixin adds a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.