title: Ruby rails - undefined method `model_name' for NilClass:Class
body: <p>I am trying to make a small search form to search a user in ldap. It doesn't really have anything to do with local data model. Not sure why do I need any changes in the user controller. The error is not giving any clue to me. </p>
  
  <p>Model - function to search in ldap </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :user_id, :firstname, :lastname, :email, :role, :misc, :password
  
    validates_presence_of :user_id, :firstname, :lastname, :email, :role, :on =&gt; :create
    validates_uniqueness_of :user_id, :email
  
    ROLES = ['Admin','User']
  
  ####################
    SERVER = '10.10.10.1'   
    PORT = 389                    
    BASE = 'DC=User,DC=mysite,DC=com'   
    DOMAIN = 'ldap.mysite.com'       
  ####################
  
    def self.ActiveDirectoryAuthenticate(login, pass)
       user = find_by_user_id(login)
  
       if user
       nil
       else
       return false
       end
          conn = Net::LDAP.new :host =&gt; SERVER,
                           :port =&gt; PORT,
                           :base =&gt; BASE,
                           :auth =&gt; { :username =&gt; "#{login}@#{DOMAIN}",
                                      :password =&gt; pass,
                                      :method =&gt; :simple }
          if conn.bind
          return user
          else
          return false
          end
          rescue Net::LDAP::LdapError =&gt; e
          return false
  end
  
    def self.findActiveDirectory(login)
          conn = Net::LDAP.new :host =&gt; SERVER,
                           :port =&gt; PORT,
                           :base =&gt; BASE,
                           :auth =&gt; { :username =&gt; 'admin',
                                      :password =&gt; 'adminpass',
                                      :method =&gt; :simple }
  
  if  conn.bind
          conn.search(:base =&gt; BASE, :filter =&gt; Net::LDAP::Filter.eq( "sAMAccountName", login ),
          :attributes =&gt; ['givenName','SN','mail'], :return_result =&gt; true) do |entry|
          entry.each do |attributes, values|
  
          if "#{attributes}" == "sn"
          values.each do |value|
          puts "Lastname: "+"#{value}"
          $lastname = "#{value}"
          end
          end
  
          if "#{attributes}" == "givenname"
          values.each do |value|
          puts "Firstname: "+"#{value}"
          $firstname = "#{value}"
          end
          end
  
          if "#{attributes}" == "mail"
          values.each do |value|
          puts "Email: "+"#{value}"
          $email = "#{value}"
          end
          end
  
          end
          end
  
      return true
      else
      return false
      end
  
      rescue Net::LDAP::LdapError =&gt; e
      return false
     end
  
  end
  </code></pre>
  
  <p>User controller - </p>
  
  <pre><code>class UsersController &lt; ApplicationController
    def new
      @user = User.new
    end
  
    def create
      @user = User.new(params[:user])
      if @user.save
        redirect_to users_added_url, :notice =&gt; "Signed up!"
      else
        render "new"
      end
    end
  end
  </code></pre>
  
  <p>View - is NOT working (app/views/users/find.html.erb)</p>
  
  <pre><code>&lt;h1&gt;Find User&lt;/h1&gt;
  
  &lt;%= form_for @user do |f| %&gt;
    &lt;% if @user.errors.any? %&gt;
      &lt;div class="error_messages"&gt;
        &lt;h2&gt;Form is invalid&lt;/h2&gt;
        &lt;ul&gt;
          &lt;% for message in @user.errors.full_messages %&gt;
            &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
    &lt;p&gt;
      &lt;%= f.label :Username %&gt;&lt;br /&gt;
      &lt;%= f.text_field :username %&gt;
    &lt;/p&gt;
    &lt;p class="button"&gt;&lt;%= f.submit %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>View - is working (app/views/users/new.html.erb)</p>
  
  <pre><code>&lt;h1&gt;Add new user&lt;/h1&gt;
  
  &lt;%= form_for @user do |f| %&gt;
    &lt;% if @user.errors.any? %&gt;
      &lt;div class="error_messages"&gt;
        &lt;h2&gt;Form is invalid&lt;/h2&gt;
        &lt;ul&gt;
          &lt;% for message in @user.errors.full_messages %&gt;
            &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
   &lt;p&gt;
      &lt;%= f.label :Username %&gt;&lt;br /&gt;
      &lt;%= f.text_field :user_id %&gt;
    &lt;/p&gt;
   &lt;p&gt;
      &lt;%= f.label :Firstname %&gt;&lt;br /&gt;
      &lt;%= f.text_field :firstname %&gt;
    &lt;/p&gt;
   &lt;p&gt;
      &lt;%= f.label :Lastname %&gt;&lt;br /&gt;
      &lt;%= f.text_field :lastname %&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;p&gt;
      &lt;%= f.label :Role %&gt;&lt;br /&gt;
      &lt;%= f.collection_select :role, User::ROLES, :to_s, :to_s, :include_blank =&gt; false %&gt;
    &lt;/p&gt;
    &lt;p class="button"&gt;&lt;%= f.submit %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>routes - </p>
  
  <pre><code># rake routes
    myapp_new GET    /myapp/new(.:format)          myapp#new
          root        /                            home#index
  sessions_new GET    /sessions/new(.:format)      sessions#new
     users_new GET    /users/new(.:format)         users#new
   users_added GET    /users/added(.:format)       users#added
        myapp GET    /myapp(.:format)              myapp#new
       log_out GET    /log_out(.:format)           sessions#destroy
        log_in GET    /log_in(.:format)            sessions#new
       sign_up GET    /sign_up(.:format)           users#new
    users_find GET    /users/find(.:format)        users#find
          root        /                            users#new
         users GET    /users(.:format)             users#index
               POST   /users(.:format)             users#create
      new_user GET    /users/new(.:format)         users#new
     edit_user GET    /users/:id/edit(.:format)    users#edit
          user GET    /users/:id(.:format)         users#show
               PUT    /users/:id(.:format)         users#update
               DELETE /users/:id(.:format)         users#destroy
      sessions GET    /sessions(.:format)          sessions#index
               POST   /sessions(.:format)          sessions#create
   new_session GET    /sessions/new(.:format)      sessions#new
  edit_session GET    /sessions/:id/edit(.:format) sessions#edit
       session GET    /sessions/:id(.:format)      sessions#show
               PUT    /sessions/:id(.:format)      sessions#update
               DELETE /sessions/:id(.:format)      sessions#destroy
  </code></pre>
  
  <p>Error -</p>
  
  <pre><code>undefined method `model_name' for NilClass:Class
  </code></pre>