You have a nil object when you didn't expect it!

Dear friends,

I get the following error when I goto the privilege page.

NoMethodError in Employee#edit_privilege

Showing app/views/employee/edit_privilege.html.erb where line #82
raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Extracted source (around line #82):

79:


80:
81: <% @privilege_tags.each_with_index do
|privilege_tag,ind| %>
82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>“priority
ASC”).select{|p| p.applicable?} %>
83: <% if all_privileges.count > 0 %>
84: <% if ind==0 or ind==1 or ind==4 %>
85:

The development log is below:

Processing EmployeeController#edit_privilege (for 127.0.0.1 at
2016-03-26 13:49:00) [GET]
Parameters: {“action”=>“edit_privilege”, “controller”=>“employee”,
“id”=>“emp-0002”}
[4;35;1mUser Load (1.0ms)[0m [0mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
configurations WHERE (configurations.config_key = ‘Locale’) LIMIT
1[0m
[4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM
configurations WHERE (configurations.config_key =
‘InstitutionType’) LIMIT 1[0m
Expired fragment: views/News_latest_fragment (0.0ms)
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
Username : admin Role : Admin
[4;35;1mConfiguration Load (0.0ms)[0m [0mSELECT * FROM
configurations WHERE (configurations.config_key =
‘StudentAttendanceType’) LIMIT 1[0m
[4;36;1mConfiguration Load (0.0ms)[0m [0;1mSELECT * FROM
configurations WHERE (configurations.config_key =
‘AvailableModules’) [0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT * FROM users WHERE
(users.id = 1) [0m
[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
configurations WHERE (configurations.config_key =
‘FirstTimeLoginEnable’) LIMIT 1[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
configurations WHERE (configurations.config_value = ‘HR’) LIMIT
1[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;36;1mPrivilege Load (0.0ms)[0m [0;1mSELECT * FROM privileges
INNER JOIN privileges_users ON privileges.id =
privileges_users.privilege_id WHERE (privileges_users.user_id = 1 )
[0m
[4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM
configurations WHERE (configurations.config_key =
‘PrecisionCount’) LIMIT 1[0m
[4;36;1mUser Load (1.0ms)[0m [0;1mSELECT * FROM users WHERE
(username LIKE BINARY(‘emp-0002’)) AND (users.is_deleted = 0) LIMIT
1[0m
[4;35;1mEmployee Load (1.0ms)[0m [0mSELECT * FROM employees WHERE
(employees.user_id = 4) LIMIT 1[0m
[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
configurations WHERE (configurations.config_value = ‘Finance’)
LIMIT 1[0m
[4;35;1mSmsSetting Columns (2.0ms)[0m [0mSHOW FIELDS FROM
sms_settings[0m
[4;36;1mSmsSetting Load (0.0ms)[0m [0;1mSELECT * FROM sms_settings
WHERE (sms_settings.settings_key = ‘ApplicationEnabled’) LIMIT 1[0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM configurations WHERE
(configurations.config_value = ‘HR’) LIMIT 1[0m
[4;36;1mPrivilegeTag Load (0.0ms)[0m [0;1mSELECT * FROM
privilege_tags ORDER BY priority ASC[0m
Rendering template within layouts/application
Rendering employee/edit_privilege
[4;35;1mEmployee Columns (3.0ms)[0m [0mSHOW FIELDS FROM
employees[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM privileges INNER JOIN
privileges_users ON privileges.id = privileges_users.privilege_id
WHERE (privileges_users.user_id = 1 ) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM privileges INNER JOIN
privileges_users ON privileges.id = privileges_users.privilege_id
WHERE (privileges_users.user_id = 1 ) [0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM privileges INNER JOIN
privileges_users ON privileges.id = privileges_users.privilege_id
WHERE (privileges_users.user_id = 1 ) [0m
[4;36;1mPrivilegeTag Columns (1.0ms)[0m [0;1mSHOW FIELDS FROM
privilege_tags[0m
[4;35;1mPrivilege Load (0.0ms)[0m [0mSELECT * FROM privileges
WHERE (name<>‘FinanceControl’) AND (privileges.privilege_tag_id = 2)
ORDER BY priority ASC[0m
[4;36;1mPrivilege Columns (2.0ms)[0m [0;1mSHOW FIELDS FROM
privileges[0m

ActionView::TemplateError (You have a nil object when you didn’t expect
it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]) on line #82 of
app/views/employee/edit_privilege.html.erb:
79:


80:
81: <% @privilege_tags.each_with_index do
|privilege_tag,ind| %>
82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>“priority
ASC”).select{|p| p.applicable?} %>
83: <% if all_privileges.count > 0 %>
84: <% if ind==0 or ind==1 or ind==4 %>
85:
app/models/privilege.rb:24:in `applicable?'
app/views/employee/edit_privilege.html.erb:82
app/views/employee/edit_privilege.html.erb:82:in `select'
app/views/employee/edit_privilege.html.erb:82
C:/Ruby187/lib/ruby/gems/1.8/gems/i18n-0.4.2/lib/i18n/backend/base.rb:42:in

each_with_index' app/views/employee/edit_privilege.html.erb:81:ineach’
app/views/employee/edit_privilege.html.erb:81:in each_with_index' app/views/employee/edit_privilege.html.erb:81 app/views/employee/edit_privilege.html.erb:71 app/controllers/application_controller.rb:361:inrender’

Rendered rescues/_trace (198.0ms)
Rendered rescues/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)

and my users table has the following record.

username: emp-0002
first_name: Adam
password: xxxxxxxx
etc.

can any one help me pls what this error means or what is it asking from
me.

On 26 March 2016 at 09:30, Naveed A. [email protected] wrote:

You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

app/models/privilege.rb:24:in `applicable?'

The error says you have called the method [] on an object that is nil.
You don’t seem to have shown us line 24 of privilege.rb (and the
surrounding lines).

Colin

Colin L. wrote in post #1182464:

On 26 March 2016 at 09:30, Naveed A. [email protected] wrote:

You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

app/models/privilege.rb:24:in `applicable?'

The error says you have called the method [] on an object that is nil.
You don’t seem to have shown us line 24 of privilege.rb (and the
surrounding lines).

Colin

class Privilege < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :privilege_tag

def applicable?
role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]
role_plugin_hash.nil? ? false : (role_plugin_hash.include? nil) ?
true : (role_plugin_hash & FedenaPlugin.accessible_plugins).present? ?
true : false
end
end

On 26 March 2016 at 09:55, Naveed A. [email protected] wrote:

Colin

class Privilege < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :privilege_tag

def applicable?
role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]

You have not told us which is line 24, but I assume it is the one
above, in which case roles_plugin_hash is nil.

role_plugin_hash.nil? ? false : (role_plugin_hash.include? nil) ?

true : (role_plugin_hash & FedenaPlugin.accessible_plugins).present? ?
true : false

That is horrible. A classic case of saving a few lines of code and
making it difficult to understand what the code is doing. I just hope
your automated tests check all the possible conditions on this.

Colin

Colin

Sorry below is line 24

role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]

So Authorization::Engine.instance.roles_plugin_hash is nil.

Colin

No idea what to do

On 26 March 2016 at 10:15, Naveed A. [email protected] wrote:

Colin

Sorry below is line 24

role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]

So Authorization::Engine.instance.roles_plugin_hash is nil.

Colin

On 26 March 2016 at 11:13, Naveed A. [email protected] wrote:

So Authorization::Engine.instance.roles_plugin_hash is nil.

Colin

No idea what to do

It depends on whether that is ever supposed to be nil. If it is then
you just need to test for that in your code before using it.

Colin

It depends on whether that is ever supposed to be nil. If it is then
you just need to test for that in your code before using it.

Colin

In my controller’s edit_privilege method I changed the below line

from
@user = User.active.first(:conditions => [“username LIKE
BINARY(?)”,params[:id]])
to
@user = User.active.find_by_username(params[:id])

and it solved the problem. but dont know why not the above line working?

def edit_privilege
@user = User.active.find_by_username(params[:id])
#User.active.first(:conditions => [“username LIKE
BINARY(?)”,params[:id]])
@employee = @user.employee_record
@privilege_tags=PrivilegeTag.find(:all,:order=>“priority ASC”)
@[email protected]
if request.post?
new_privileges = params[:user][:privilege_ids] if params[:user]
new_privileges ||= []
@user.privileges = Privilege.find_all_by_id(new_privileges)
redirect_to :action => ‘admission4’,:id => @employee.id
end
end

Colin,

User.active.first(:conditions => ["username LIKE ?, params[:id])

The above code is wrong. here your are trying to fetch the list of the
active users and fetching the first record
then checking the username with id value. the entire code seems wrong

also in params[:id] you have to get ID value and not the name, In that
case
you are passing the value wrong in form

Thanks,

Asvini Ramesh

On Saturday, March 26, 2016 at 3:00:46 PM UTC+5:30, Ruby-Forum.com User

Asvini Ramesh wrote in post #1182593:

On Saturday, March 26, 2016 at 3:00:46 PM UTC+5:30, Ruby-Forum.com User

Well in Production mode it works fine but in Development mode it show
the above error.

On 8 April 2016 at 07:17, Naveed A. [email protected] wrote:

Asvini Ramesh wrote in post #1182593:

On Saturday, March 26, 2016 at 3:00:46 PM UTC+5:30, Ruby-Forum.com User

Well in Production mode it works fine but in Development mode it show
the above error.

What works fine? You have not shown us what the current code is.
Also show the full error message and the line of code giving the error
(with a few lines either side).

This is a mailing list not a forum (though you may be accessing it via
a forum like interface), so you must not force us to look back through
previous messages trying to work out what you are talking about,
particularly when your previous message was over a week ago.

Colin

On 31 March 2016 at 10:05, Naveed A. [email protected] wrote:

BINARY(?)",params[:id]])
to
@user = User.active.find_by_username(params[:id])

and it solved the problem. but dont know why not the above line working?

I would not have expected either to work, as I would have expected
params[:id] to contain the id of the user not the name. What is in
params[:id]?

Colin

Colin

I get the following error when I goto the privilege page.

NoMethodError in Employee#edit_privilege

Showing app/views/employee/edit_privilege.html.erb where line #82
raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Extracted source (around line #82):

79:


80:
81: <% @privilege_tags.each_with_index do
|privilege_tag,ind| %>
82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>“priority
ASC”).select{|p| p.applicable?} %>
83: <% if all_privileges.count > 0 %>
84: <% if ind==0 or ind==1 or ind==4 %>
85:

The development log is below:

Processing EmployeeController#edit_privilege (for 127.0.0.1 at
2016-03-26 13:49:00) [GET]
Parameters: {“action”=>“edit_privilege”, “controller”=>“employee”,
“id”=>“emp-0002”}
[4;35;1mUser Load (1.0ms)[0m [0mSELECT * FROM users WHERE
(users.id = 1) AND (users.is_deleted = 0) [0m
[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
configurations WHERE (configurations.config_key = ‘Locale’) LIMIT
1[0m

edit_privilge method in controller is below

def edit_privilege
@user = User.active.first(:conditions => [“username LIKE
BINARY(?)”,params[:id]])
@employee = @user.employee_record
@privilege_tags=PrivilegeTag.find(:all,:order=>“priority ASC”)
@[email protected]
if request.post?
new_privileges = params[:user][:privilege_ids] if params[:user]
new_privileges ||= []
@user.privileges = Privilege.find_all_by_id(new_privileges)
redirect_to :action => ‘admission4’,:id => @employee.id
end
end

On 8 April 2016 at 12:51, Naveed A. [email protected] wrote:

You have a nil object when you didn’t expect it!

privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>"priority

ASC").select{|p| p.applicable?} %>

You can get a better handle on this by splitting the above lines into
multiple lines, so something like
<% all_privileges
=privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>“priority
ASC”) %>
<% all_privileges = all_privileges.select{|p| p.applicable?} %>

Then you can find which bit is causing the error.

What is p.applicable?

Colin