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:
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).
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
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.
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
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.