title: ActiveRecord::RecordNotFound -- Couldn't find User without an ID
body: <p>Please see UPDATES at the bottom of the question...</p>
  
  <p>For reference, this problem evolved out of some fixes I made based on a previous problem I was having here: <a href="https://stackoverflow.com/questions/1298846/associating-two-models-in-rails-user-and-profile">Associating Two Models in Rails (user and profile)</a></p>
  
  <p>I'm building an app that has a user model and a profile model.</p>
  
  <p>I want to associate these models such that:<br>
  - After the user creates an account, he is automatically sent to the "create profile" page, and the profile he creates is connected to only that particular user.<br>
  - Only the user who owns the profile can edit it.</p>
  
  <p>I generated the user model using nifty_generators. When the user hits submit for the account creation, I redirect him to the "new profile" view to create a profile. I did this by editing the redirect path in the user controller. The user controller looks like this:</p>
  
  <pre><code>def new
    @user = User.new
  end
  
  def create
    @user = User.new(params[:user])
    if @user.save
      session[:user_id] = @user.id
      flash[:notice] = "Thank you for signing up! You are now logged in."
      redirect_to new_user_profile_path(:user_id =&gt; @user)
    else
      render :action =&gt; 'new'
    end
  end
  </code></pre>
  
  <p>When I click 'submit' to create a new user, it now sends me to the following url, which seems right: localhost:3000/users/6/profile/new.  But it throws the following exception:</p>
  
  <blockquote>
    <p>NoMethodError in
    ProfilesController#new   You have a
    nil object when you didn't expect it! 
    The error occurred while evaluating
    nil.build</p>
  </blockquote>
  
  <p>The trace indicates that the problem is in the profiles controller, in the New method.  The profiles controller looks like this:</p>
  
  <pre><code>def index
    @user = User.find(params[:user_id])
    @profile = @user.profile(:order =&gt; "created_at DESC")
  end
  
  def show
    @user = User.find(params[:user_id])
    @profile = @user.profile.find(params[:id])
  end
  
  def new
    @user = User.find(params[:user_id])
    @profile = @user.profile.build
  end
  
  def edit
    @user = User.find(params[:user_id])
    @profile = @user.profile.find(params[:id])
  end
  
  def create
    @user = User.find(params[:user_id])
    @profile = @user.profile.build(params[:profile])
      if @profile.save
        flash[:notice] = 'Profile was successfully created.'
        redirect_to(@profile)
      else
        flash[:notice] = 'Error.  Something went wrong.'
        render :action =&gt; "new"
      end
  end
  </code></pre>
  
  <p>Additionally, the app also throws an exception when I try to view the index page of the profiles (there are currently no profiles because I can't get past the user creation step to create one).  This is the exception:<br>
  ActiveRecord::RecordNotFound in ProfilesController#index<br>
  Couldn't find User without an ID  </p>
  
  <p>This is what the log is telling me:    </p>
  
  <blockquote>
    <p>Processing ProfilesController#index
    [GET]       Parameters:
    {"action"=>"index",
    "controller"=>"profiles"}    </p>
    
    <p>ActiveRecord::RecordNotFound (Couldn't
    find User without an ID):<br>
    app/controllers/profiles_controller.rb:5:in
    `index'</p>
  </blockquote>
  
  <p>To give you the rest of the details on the app, the models have the following associations:<br>
      Profile belongs _to :user<br>
      User has _one :profile  </p>
  
  <p>I have this in the routes.rb file: map.resources :users, :has_one => :profile  </p>
  
  <p>In the view for the new profile page that's throwing the first exception listed above, I have this:</p>
  
  <pre><code>&lt;% form_for([@user, @profile]) do |f| %&gt;
    &lt;%= f.error_messages %&gt;
  ....
  &lt;% end %&gt;  
  </code></pre>
  
  <p>In the view for the profile index that's throwing the second exception explained above, I have this:</p>
  
  <pre><code>&lt;% @profiles.each do |profile| %&gt;
  &lt;div class="post"&gt;
      &lt;div class="left"&gt;
          &lt;p&gt;Store: &lt;/p&gt;
          &lt;p&gt;Category: &lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="right"&gt;
          &lt;p&gt;&lt;%=h profile.name %&gt;&lt;/p&gt;
          &lt;p&gt;&lt;%=h profile.category %&gt;&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="bottom"&gt;
          &lt;p&gt;&lt;%= link_to 'Go to profile', user_profile_path(@user, profile) %&gt;&lt;/p&gt;
          &lt;p&gt;&lt;%= link_to 'Edit', edit_user_profile_path(@user, profile) %&gt;&lt;/p&gt;
          &lt;p&gt;&lt;%= link_to 'Destroy', user_profile_path(@user, profile), :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;&lt;/p&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p> </p>
  
  
  
  <p>I've spent hours trying to track down the problem myself as a learning exercise, but at this point I have no idea how to fix this.  Appreciate the help!</p>
  
  <h1>UPDATE:  </h1>
  
  <p>jdl, per your request:<br>
  profiles/new.html.erb:  </p>
  
  <pre><code>&lt;% form_for([@user, @profile]) do |f| %&gt;
    &lt;%= f.error_messages %&gt;
  
  
  
  &lt;div class="left"&gt;
    &lt;p&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;required
    &lt;/p&gt;
      &lt;p&gt;
      &lt;%= f.label :category %&gt;&lt;br /&gt;
      &lt;%= f.text_field :category %&gt;required
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :address1 %&gt;&lt;br /&gt;
      &lt;%= f.text_field :address1 %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :address2 %&gt;&lt;br /&gt;
      &lt;%= f.text_field :address2 %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :city %&gt;&lt;br /&gt;
      &lt;%= f.text_field :city %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :state %&gt;&lt;br /&gt;
      &lt;%= f.text_field :state %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :zip %&gt;&lt;br /&gt;
      &lt;%= f.text_field :zip %&gt;required
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :phone %&gt;&lt;br /&gt;
      &lt;%= f.text_field :phone %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :email %&gt;&lt;br /&gt;
      &lt;%= f.text_field :email %&gt;
    &lt;/p&gt;
  
    &lt;/div&gt;
  
    &lt;div class="right"&gt;
    &lt;p&gt;
      &lt;%= f.label :website %&gt;&lt;br /&gt;
      &lt;%= f.text_field :website %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :description %&gt;&lt;br /&gt;
      &lt;%= f.text_area :description %&gt;
    &lt;/p&gt;
    &lt;/div&gt;
  
    &lt;p&gt;
      &lt;%= f.submit 'Create' %&gt;
    &lt;/p&gt;
      &lt;% end %&gt; 
  </code></pre>
  
  <p>routes.rb:<br>
      ActionController::Routing::Routes.draw do |map|<br>
        map.signup 'signup', :controller => 'users', :action => 'new'<br>
        map.logout 'logout', :controller => 'sessions', :action => 'destroy'<br>
        map.login 'login', :controller => 'sessions', :action => 'new'<br>
        map.resources :sessions</p>
  
  <pre><code>  map.resources :users, :has_one =&gt; :profile  
  
    map.root :controller =&gt; "home"   
    map.connect ':controller/:action/:id'  
    map.connect ':controller/:action/:id.:format'  
  end  
  </code></pre>
  
  <p>PROFILES CONTROLLER (as of 8/20/09, 8pm EST)<br>
      class ProfilesController &lt; ApplicationController</p>
  
  <pre><code>  def index
      @users = User.all(:order =&gt; "created_at DESC")
    end
  
    def show
      @user = User.find(params[:user_id])
    end
  
    def new
      @user.profile = Profile.new
    end
  
    def edit
      @user = User.find(params[:user_id])
      @profile = @user.profile.find(params[:id])
    end
  
    def create
      @user = User.find(params[:user_id])
      @profile = @user.profile.build(params[:profile])
        if @profile.save
          flash[:notice] = 'Profile was successfully created.'
          redirect_to(@profile)
        else
          flash[:notice] = 'Error.  Something went wrong.'
          render :action =&gt; "new"
        end
    end
  
    def update
      @profile = Profile.find(params[:id])
        if @profile.update_attributes(params[:profile])
          flash[:notice] = 'Profile was successfully updated.'
          redirect_to(@profile)
        else
          render :action =&gt; "edit"
        end
    end
  
    def destroy
      @profile = Profile.find(params[:id])
      @profile.destroy
        redirect_to(profiles_url)
    end
  end
  </code></pre>
  
  <p>Cody, the index page below is throwing the following exception:<br>
  NoMethodError in Profiles#index<br>
  Showing app/views/profiles/index.html.erb where line #14 raised:<br>
  undefined method `name' for #  </p>
  
  <pre><code>    &lt;div id="posts"&gt;
  &lt;% @users.each do |profile| %&gt;
      &lt;div class="post"&gt;
          &lt;div class="left"&gt;
              &lt;p&gt;Store: &lt;/p&gt;
              &lt;p&gt;Category: &lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="right"&gt;
              &lt;p&gt;&lt;%=h profile.name %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%=h profile.category %&gt;&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="bottom"&gt;
              &lt;p&gt;&lt;%= link_to 'Go to profile', user_profile_path(@user, profile) %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%= link_to 'Edit', edit_user_profile_path(@user, profile) %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%= link_to 'Destroy', user_profile_path(@user, profile), :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;&lt;/p&gt;
          &lt;/div&gt;
    &lt;/div&gt; 
  </code></pre>