正在处理新的查找视图作为新的创建请求

时间:2012-04-05 23:55:27

标签: ruby-on-rails ruby ruby-on-rails-3

我之前有一个问题,我正在尝试制作一个ldap搜索表单。 到目前为止,我为用户/查找做了rails generate。在模型中,我有一个在ldap中搜索用户的功能,它在rails之外独立工作。 但是,通过此视图的请求实际上被视为创建新用户的请求,而只是在ldap中搜索用户。 我是rails的新手,不是缺少的链接。需要一些帮助来理解这一点,将来会有很多这样的功能/功能我必须在这个测试应用中添加。我认为这可能会导致同样的问题。

# rails generate controller users find

错误 -

undefined method `gsub' for nil:NilClass
Started GET "/users/find" for 10.85.41.23 at 2012-04-05 19:56:27 -0400
Processing by UsersController#find as HTML
Completed 500 Internal Server Error in 15ms

NoMethodError (undefined method `gsub' for nil:NilClass):
  app/models/user.rb:54:in `FindActiveDirectory'
  app/controllers/users_controller.rb:10:in `find'

模型 -

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 find
   @user = User.FindActiveDirectory(params[:user_id])
  end

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

end

查看 -

<h1>Users#find</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 class="button"><%= f.submit %></p>
<% end %>

路线 -

rubyapp::Application.routes.draw do

  get "users/find"

get "myapp/new"

root :to => "sessions#new"
#root :to => "home#index"
get "sessions/new"
get "users/new"
get "users/added" => "users#added"
get "myapp" => "myapp#new"
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"

resources :users
resources :sessions
 end

1 个答案:

答案 0 :(得分:1)

您需要另一种方法来处理您返回的数据:

控制器:

def find
end

def display_result
  @result = User.findActiveDirectory( params[:user_id] )

  if @result.empty?
    render action: "find", notice: "Could not find a user with id #{params[:user_id]}"
  end
end

下一步是添加路由到routes.rb:

get 'users/find'
post 'users/display_result'

现在我们必须更新find的视图:

<h1>Users#find</h1>

<p><%= notice %></p>

<%= form_tag users_display_result_path do %>
  <p>
    <%= label_tag :Username %><br />
    <%= text_field_tag :user_id %>
  </p>
  <p class="button"><%= submit_tag %></p>
<% end %>

并创建用于显示结果的新视图(这个是非常基本的,我想你需要对此进行很多改进,但这应该会给你一个想法):

<h1>Users#display_result</h1>
<%= debug @result %>

最后但并非最不重要的是改变模型中的一些东西:

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

  if  conn.bind
    result = HashWithIndifferentAccess.new

    conn.search( :base => BASE,
                 :filter => Net::LDAP::Filter.eq( "sAMAccountName", login ),
                 :attributes => ['givenName','SN','mail'],
                 :return_result => true
    ) do |entries|

      entries.each do |attribute, value|
        result[attribute] = value
      end
  end

  return result

  rescue Net::LDAP::LdapError => e
    return false
end

您将在控制器/视图中使用名为@result的变量结束。此变量是一个哈希,其属性为键。所以你可以在视图中做这样的事情:

<% @result.each do |key,value| %>
  <%= key.to_s.normalize + ": " + value.to_s %>
<% end >