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

39 views
Skip to first unread message

Naveed Alam

unread,
Mar 26, 2016, 5:30:46 AM3/26/16
to rubyonra...@googlegroups.com
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: <table width="100%" align="center" cellspacing="1"
cellpadding="1" class="table_design">
80: <tr>
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: <td>



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: <table width="100%" align="center" cellspacing="1"
cellpadding="1" class="table_design">
80: <tr>
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: <td>

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:in `each'
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:in `render'

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.

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Mar 26, 2016, 5:42:36 AM3/26/16
to Ruby on Rails: Talk
On 26 March 2016 at 09:30, Naveed Alam <li...@ruby-forum.com> wrote:
> 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.[]
>
> 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

Naveed Alam

unread,
Mar 26, 2016, 5:56:22 AM3/26/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182464:
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

Colin Law

unread,
Mar 26, 2016, 6:08:38 AM3/26/16
to Ruby on Rails: Talk
On 26 March 2016 at 09:55, Naveed Alam <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1182464:
>> On 26 March 2016 at 09:30, Naveed Alam <li...@ruby-forum.com> 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]

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

Naveed Alam

unread,
Mar 26, 2016, 6:15:57 AM3/26/16
to rubyonra...@googlegroups.com
Colin

Sorry below is line 24

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

Colin Law

unread,
Mar 26, 2016, 6:35:23 AM3/26/16
to Ruby on Rails: Talk
On 26 March 2016 at 10:15, Naveed Alam <li...@ruby-forum.com> 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

Naveed Alam

unread,
Mar 26, 2016, 7:14:13 AM3/26/16
to rubyonra...@googlegroups.com
>
> So Authorization::Engine.instance.roles_plugin_hash is nil.
>
> Colin

No idea what to do

Colin Law

unread,
Mar 26, 2016, 7:37:24 AM3/26/16
to Ruby on Rails: Talk
On 26 March 2016 at 11:13, Naveed Alam <li...@ruby-forum.com> 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

Naveed Alam

unread,
Mar 31, 2016, 5:06:21 AM3/31/16
to rubyonra...@googlegroups.com
> 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")
@user_privileges=@user.privileges
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 Law

unread,
Mar 31, 2016, 6:27:28 AM3/31/16
to Ruby on Rails: Talk
On 31 March 2016 at 10:05, Naveed Alam <li...@ruby-forum.com> wrote:
>> 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?

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

Asvini Ramesh

unread,
Apr 1, 2016, 10:09:28 AM4/1/16
to Ruby on Rails: Talk

Asvini Ramesh

unread,
Apr 1, 2016, 10:09:33 AM4/1/16
to Ruby on Rails: Talk
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

Naveed Alam

unread,
Apr 8, 2016, 2:18:37 AM4/8/16
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Apr 8, 2016, 3:16:51 AM4/8/16
to Ruby on Rails: Talk
On 8 April 2016 at 07:17, Naveed Alam <li...@ruby-forum.com> 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

Naveed Alam

unread,
Apr 8, 2016, 7:52:25 AM4/8/16
to rubyonra...@googlegroups.com
>
> Colin
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")
@user_privileges=@user.privileges
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 Law

unread,
Apr 8, 2016, 8:41:57 AM4/8/16
to Ruby on Rails: Talk
On 8 April 2016 at 12:51, Naveed Alam <li...@ruby-forum.com> wrote:
>>
>> 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: <table width="100%" align="center" cellspacing="1"
> cellpadding="1" class="table_design">
> 80: <tr>
> 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?} %>

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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/926003e903b01ebb2f201a77d2ac328d%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages