title: Confused as to why I am getting a nil error on a new user form before it has been called
body: <p>I will try my best to be clear, I myself am confused as to what is happening or how to explain it. I am building a Rails app.</p>
  
  <p>I have two forms so far, both in modals, which are in partials. 
  I have my app rooted to a path called home, which is defined in application controller. In my html.erb, I want a particular styling on that page, so I have set it so that if I am on the home page, the body tag gets a class of 'homepage'. Here is the code from application_controller.rb:</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
      protect_from_forgery with: :exception
  
      include SessionsHelper
  
      def home
          @home_page = true
           # @user = User.new
      end
  
  end
  </code></pre>
  
  <p>As you can see, I have a code <code>@user = User.new</code> commented out. I added that because without it, Rails throws an error <code>First argument in form cannot contain nil or be empty</code> in reference to my signup form which in in a module, which is in a partial. Here is that form:</p>
  
  <pre><code> &lt;div class="modal fade" id="loginModal" role="dialog"&gt;
     &lt;div class="modal-dialog"&gt;
  
        &lt;!-- Modal content--&gt;
        &lt;div class="modal-content"&gt;
  
          &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal"&gt;&amp;times;&lt;/button&gt;
              &lt;h2 class="modal-title"&gt;Welcome Back!&lt;/h2&gt;
          &lt;/div&gt;
  
                &lt;div class="modal-body"&gt;
  
                    &lt;%= form_for(:session, url: sessions_path) do |f| %&gt;
                        &lt;%= label_tag :username %&gt;:
                        &lt;%= f.text_field :user_name, class: 'form-control'%&gt;
                        &lt;br&gt;
                        &lt;%= label_tag :password%&gt;:
                        &lt;%= f.password_field :password, class: 'form-control'%&gt;
                        &lt;br&gt;&lt;br&gt;
                        &lt;%= submit_tag "Login", class: 'login-button'%&gt;
                    &lt;% end %&gt;
  
                &lt;/div&gt; &lt;!-- modal body --&gt;
  
            &lt;div class="modal-footer"&gt;
                &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
            &lt;/div&gt;
  
        &lt;/div&gt; &lt;!-- modal-content --&gt;
      &lt;/div&gt; &lt;!-- modal-dialog --&gt;
    &lt;/div&gt; &lt;!-- login-modal --&gt;
  </code></pre>
  
  <p>If uncomment that line in the application controller, I can go to the home page without errors. But then it messes up sessions, because from what I can tell, I have some new user waiting to be created, as that line is invoking the form I suppose. In addition, from terminal, I can see it is also finding user 1 for some reason. Here is part of the terminal output if it helps:</p>
  
  <pre><code>Started GET "/" for ::1 at 2015-08-19 23:35:30 -0400
    ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
  Processing by ApplicationController#home as HTML
    Rendered application/home.html.erb within layouts/application (2.2ms)
    User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
    Rendered users/_log_in_form.html.erb (14.2ms)
    Rendered users/_new.html.erb (6.2ms)
  Completed 500 Internal Server Error in 583ms (ActiveRecord: 6.2ms)
  
  ArgumentError - First argument in form cannot contain nil or be empty:
    actionview (4.2.3) lib/action_view/helpers/form_helper.rb:432:in `form_for'
    app/views/users/_new.html.erb:15:in `_app_views_users__new_html_erb___2821927674300231403_70140243283980'
    actionview (4.2.3) lib/action_view/template.rb:145:in `block in render'
  </code></pre>
  
  <p>so when I do log in, despite my user id being 1 in the database, it is being shown in terminal as 2 - which throws a bunch of undefined errors etc when I try to log my username or session[:user_id] in console(because ther IS no user 2). Of course, putting that <code>@user = User.new</code> line is wrong and I know this, but without it I get an error on the form as soon as I go to the home page, it seems to need to have the @user available...but with it there I get errors in sessions and cant log in or out properly...what is going on here? Here is the part of my application.html.erb that is rendering the forms, in case it helps. Also note that when I log in, I get no errors and the logout link appears, but I am unable to log out (more errors) and as I said, terminal shows me as having a id of 2 when it should be 1. I have the modal's render commented out for testing.</p>
  
  <pre><code>   &lt;% if logged_in? %&gt;
                &lt;h1&gt;Welcome back &lt;%= current_user.user_name %&gt;!&lt;/h1&gt;
                &lt;%= link_to "Logout", sessions_path, method: :destroy %&gt; 
      &lt;% end %&gt;
  
      &lt;ul&gt;
        &lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt; Find a Project&lt;/li&gt;
        &lt;li lass="btn btn-info btn-lg" data-toggle="modal" data-target="#registerModal"&gt;Register&lt;/li&gt;
        &lt;li lass="btn btn-info btn-lg" data-toggle="modal" data-target="#loginModal"&gt;Login&lt;/li&gt;
      &lt;/ul&gt;
  
  
      &lt;div id='login-container'&gt;
       &lt;!--  &lt;%= render 'users/log_in_form' %&gt; --&gt;
      &lt;/div&gt;
  
      &lt;div class="signup-container"&gt;
       &lt;!--  &lt;%= render 'users/new' %&gt; --&gt;
      &lt;/div&gt;
  </code></pre>