gpt4 book ai didi

ruby-on-rails - Ruby rails - NilClass :Class 的未定义方法 `model_name'

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:00 24 4
gpt4 key购买 nike

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

模型 - 在 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

用户 Controller -

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

View - 不工作(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

最佳答案

问题是您在 View 中使用了 @user,但是 @user 未在任何地方定义,因此 nil。这就是您收到此错误的原因:

undefined method `model_name' for NilClass:Class

要解决这个问题,您必须将以下内容添加到您的 Controller 中:

def find
@user = User.new
end

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

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

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

关于ruby-on-rails - Ruby rails - NilClass :Class 的未定义方法 `model_name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10023353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com