1

我正在尝试制作一个小型搜索表单来搜索 ldap 中的用户。它与本地数据模型没有任何关系。不知道为什么我需要在用户控制器中进行任何更改。该错误没有给我任何线索。

模型 - 在 ldap 中搜索的函数

class User < ActiveRecord::Base
  attr_accessible :user_id, :firstname, :lastname, :email, :role, :misc, :password

  validates_presence_of :user_id, :firstname, :lastname, :email, :role, :on => :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 => SERVER,
                         :port => PORT,
                         :base => BASE,
                         :auth => { :username => "#{login}@#{DOMAIN}",
                                    :password => pass,
                                    :method => :simple }
        if conn.bind
        return user
        else
        return false
        end
        rescue Net::LDAP::LdapError => e
        return false
end

  def self.findActiveDirectory(login)
        conn = Net::LDAP.new :host => SERVER,
                         :port => PORT,
                         :base => BASE,
                         :auth => { :username => 'admin',
                                    :password => 'adminpass',
                                    :method => :simple }

if  conn.bind
        conn.search(:base => BASE, :filter => Net::LDAP::Filter.eq( "sAMAccountName", login ),
        :attributes => ['givenName','SN','mail'], :return_result => 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 => e
    return false
   end

end

用户控制器 -

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to users_added_url, :notice => "Signed up!"
    else
      render "new"
    end
  end
end

查看 - 不工作 (app/views/users/find.html.erb)

<h1>Find User</h1>

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <p>
    <%= f.label :Username %><br />
    <%= f.text_field :username %>
  </p>
  <p class="button"><%= f.submit %></p>
<% end %>

查看 - 正在工作 (app/views/users/new.html.erb)

<h1>Add new user</h1>

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
 <p>
    <%= f.label :Username %><br />
    <%= f.text_field :user_id %>
  </p>
 <p>
    <%= f.label :Firstname %><br />
    <%= f.text_field :firstname %>
  </p>
 <p>
    <%= f.label :Lastname %><br />
    <%= f.text_field :lastname %>
  </p>
  <p>
    <%= f.label :Email %><br />
    <%= f.text_field :email %>
  </p>
  <p>
    <%= f.label :Role %><br />
    <%= f.collection_select :role, User::ROLES, :to_s, :to_s, :include_blank => false %>
  </p>
  <p class="button"><%= f.submit %></p>
<% end %>

路线 -

# 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

错误 -

undefined method `model_name' for NilClass:Class
4

1 回答 1

11

问题是您@user在视图中使用,但@user在任何地方都没有定义,因此nil. 这就是您收到此错误的原因:

undefined method `model_name' for NilClass:Class

要解决此问题,您必须将以下内容添加到您的控制器:

def find
  @user = User.new
end

或者像这样改变你的观点:

<%= form_for User.new do |f| %>

这将消除您遇到的错误,但提交此表单将发布到您的创建操作,这不是您想要的。对于搜索,您最好使用form_tag. 查看Rails Guides了解表单基础知识。基本上form_for是用于创建和更新数据,这不适用于搜索。

于 2012-04-05T08:39:29.563 回答