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 8048845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:36:39+00:00 2026-06-05T06:36:39+00:00

In a Rails 3.2 app, when a certain form fails to save (validation fails)

  • 0

In a Rails 3.2 app, when a certain form fails to save (validation fails) and redirects back to the form I get an error:

undefined method `map' for nil:NilClass

This form does not display any errors when navigating directly to the new or edit paths.

The error is coming from a select field with a custom options_from_collection_for_select method.

<%= f.select(:user_ids, options_from_collection_for_select_with_attributes(@users, :id, :name, 'data-attributes', :attributes ), {include_blank:true}, {multiple:true}) %>

If I replace the instance variable @users with User.all then I don’t get an error after redirect.

I guess @users is empty after the redirect, hence the error. But why? @users is defined in the new and edit controllers.

My controller is:

def create
  --bunch of stuff
  if @model.save
    --bunch of stuff
    respond_to do |format|
      format.html { render :text => model_url(@model) }
      format.html { redirect_to(@model, :notice => 'Success!.') }
      format.xml  { render :xml => @model, :status => :created, :location => @model }
    end

  else
    respond_to do |format|
      format.html { render :action => "new" }
      format.xml  { render :xml => @model.errors, :status => :unprocessable_entity }
    end
  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-06-05T06:36:41+00:00Added an answer on June 5, 2026 at 6:36 am

    It’s because you don’t actually execute the “new” action if it fails. Here’s a typical controller structure

    class PotsController < ApplicationController
    
      def new
        @pot = Pot.new
        @users = User.all
      end
    
      def create
        @pot = Pot.new(params[:pot])
        if @pot.create
          redirect_to @pot, notice: "Created"
        else
          #****you are here****
          render :new
        end
      end
    end
    

    In the above, if pot.create fails, it just renders the new template. What you should do is get your instance variables in that case too

      def create
        @pot = Pot.new(params[:pot])
        if @pot.create
          redirect_to @pot, notice: "Created"
        else
          @users = User.all #this is the important line
          render :new
        end
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just found a bug in my Rails app which would lead to certain
I have a rails app. I also wrote a method in Java which constructs
I have a gallery in my rails app that needs to only allow certain
In our current rails app, we are following certain patterns for including assets such
Many shared hosts restrict you to use certain gems. When installing a Rails app
I have a rails 3 app that writes certain events to a queue. Now
I want to conditionally switch on/off certain functionality in my Rails app, for example
In my rails app, I want to render certain pages within a lightbox but
I have a Rails 3 app that I would like to tweet certain status
My rails app allows users to edit a certain json file through the browser.

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.