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 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

Related Questions

I have a create action in the controller that I use for a form
I have a form that is suppose to be a POST to CREATE action
I have a Form element: $Form=new Zend_Form; $Form->setAction($this->view->url(array('controller'=>'auth','action'=>'create'),null,true)) ->setMethod('post') ->setAttrib('id','auth-form') ->removeAttrib('enctype'); As can be
I have done the following form <% form_for @anexo, :url => {:action => create},
If i have a controller action Create that returns a view with the following
I have an action which create QMessageBox. In that dialog I want to print
I have an HttpPost controller action that takes in a simple form DTO object.
I have a create action that doesn't send the CreatedOn and CreatedBy back to
I have a simple create action that save a product to DB. after saving
I have a User.create action that provisionally registers a new user and sends an

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.