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

  • Home
  • SEARCH
  • 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 706759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:12:07+00:00 2026-05-14T04:12:07+00:00

I have a create action for a form that potentially generates errors (i.e. first

  • 0

I have a create action for a form that potentially generates errors (i.e. first name is missing) and then redirects.

The problem is, that when the redirect happens those form errors get lost.
How could I pass those form errors in a session to be displayed back in the original form (which should still be filled out with the previous details, as in the original error_messages behavior)?

Thanks!


The code:

def create
  @contact = Contact.new(params[:contact])
  if @contact.save
    flash[:notice] = "Sent."
  else
    flash[:notice] = "Error."
  end
end
  • 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-05-14T04:12:07+00:00Added an answer on May 14, 2026 at 4:12 am

    This is a tricky problem that I’ve had trouble with myself. The first question I would ask is why do you need to redirect when errors are found? Forcing you to render the action when there are errors was a conscious decision of the designers of the Rails framework due to complexity and usability concerns.

    Here’s the big issue, so in your action, you create an instance of a model using params, the object validation fails and you decide to redirect to another action. Before redirecting to another action you would have to save the current state of your model instance to the session and then redirect to action :foo. In action :foo you’d have to reattempt to update the attributes and pass the errors to the view via an instance variable. The issue here is that you’re coupling actions in your controller which is a bad thing (one action is dependent on the other). There are a host of other problems which I could type about forever, but if you only need to do this for one resource, here’s how I’d do it:

    config/routes.rb

    map.resources :things, :member => { :create_with_errors => :get }
    

    things_controller.rb

    def new
      @thing = Thing.new
    end
    
    def create
      @thing = Thing.create(params[:thing])
      if @thing.save
        redirect_to things_path
      else
        session[:thing] = @thing
        redirect_to create_errors_thing_path(@thing)
      end
    end
    
    def create_with_errors
      @thing = session[:thing]
      @errors = @thing.errors
      render :action => :new
    end
    

    app/views/things/new.html.erb

    <% if defined?(@errors) %>
    <% #do something with @errors to display the errors %>
    <% end %>
    
    <!-- render the form stuff -->
    

    I know what you’re thinking… this is hideous. Trust me, I’ve made lots of attempts to address this problem and I’ve come to realize, the mechanism the rails developers have chosen is the best and easiest way to deal with errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are either of the columns in your statement the primary… May 17, 2026 at 3:16 am
  • Editorial Team
    Editorial Team added an answer It is absolutely OK to use this static class as… May 17, 2026 at 3:16 am
  • Editorial Team
    Editorial Team added an answer First of all, in your GridView markup, you can change… May 17, 2026 at 3:16 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

On a view, i have a partial view rendering an ajax form that allows
I have an Ajax form in my Rails app that contains the Recaptcha markup
I have a method defined in my controller that I am trying to create
I have the following action servlet and was wondering if I should create a
I have an action called Sessies. In this action i am creating 'Sessies' objects
I have two views: create and edit. Both share a strongly typed editor template
I am working on a basic app that has a public facing form (for
i cannot seem to get observe_form to call a particular action i have a
I have a person model and a quotes model. Each person can have many
I have a wordpress theme with an options page. I have included a basic

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.