@employee = Employee.find(params[:id])
if request.post? and @rosterduty.save
flash[:notice] = t('flash7')
redirect_to :controller => "employee", :action => "add_rosterduty"
end
end
and below is the view, where I want to call method from the Employee
model, method name is full_name which says full_name undefined.
Can you copy/paste the full error message please.
Colin
NoMethodError in Employee#add_rosterduty
Showing app/views/employee/add_rosterduty.html.erb where line #5 raised:
undefined method `full_name' for nil:NilClass
Extracted source (around line #5):
Error messages are often a little tricky to understand but there is
often useful information there if the message is considered carefully.
Note that it is saying undefined method `full_name' for nil:NilClass.
This means that you have tried to call the method full_name on an
object that is nil. Looking at the code this means that @employee is
nil. You need to work out why that is so.
An easy way of doing simple debugging is to insert into your code lines such as
logger.info( "Employee: #{@employee.inspect} )
That will insert a line into development.log showing the value of @employee.
model, method name is full_name which says full_name undefined.
Extracted source (around line #5):
Application Trace | Framework Trace | Full Trace
{"id"=>"2"}
Error messages are often a little tricky to understand but there is
often useful information there if the message is considered carefully.
Note that it is saying undefined method `full_name' for nil:NilClass.
This means that you have tried to call the method full_name on an
object that is nil. Looking at the code this means that @employee is
nil. You need to work out why that is so.
An easy way of doing simple debugging is to insert into your code lines
such as
logger.info( "Employee: #{@employee.inspect} )
That will insert a line into development.log showing the value of
@employee.
Colin
Dont know if I am right, but I tried this and the log is below:
<%= logger.info( "Employee: #{@employee.inspect}") %>
# Logfile created on Sun Mar 06 14:49:25 +0500 2016 [4;36;1mSQL
(0.0ms)[0m [0;1mSET NAMES 'utf8'[0m
[4;35;1mSQL (0.0ms)[0m [0mSET SQL_AUTO_IS_NULL=0[0m
** vote_fu: initialized properly.
** SubdomainFu: initialized properly
[4;36;1mSQL (10.0ms)[0m [0;1mSHOW TABLES[0m
[4;35;1mSQL (0.0ms)[0m [0mSHOW TABLES[0m
[4;36;1mFeeCollectionDiscount Columns (40.0ms)[0m [0;1mSHOW FIELDS
FROM `fee_collection_discounts`[0m
[4;35;1mFeeDiscount Columns (0.0ms)[0m [0mSHOW FIELDS FROM
`fee_discounts`[0m
[4;36;1mRecordUpdate Columns (0.0ms)[0m [0;1mSHOW FIELDS FROM
`record_updates`[0m
[4;35;1mSQL (0.0ms)[0m [0mSET NAMES 'utf8'[0m
[4;36;1mSQL (0.0ms)[0m [0;1mSET SQL_AUTO_IS_NULL=0[0m
Processing EmployeeController#add_rosterduty (for 127.0.0.1 at
2016-03-06 14:50:23) [GET]
Parameters: {"action"=>"add_rosterduty", "id"=>"2",
"controller"=>"employee"}
[4;35;1mUser Columns (2.0ms)[0m [0mSHOW FIELDS FROM `users`[0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
[4;35;1mConfiguration Columns (1.0ms)[0m [0mSHOW FIELDS FROM
`configurations`[0m
[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` = 'Locale') LIMIT
1[0m
[4;35;1mConfiguration Load (0.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'InstitutionType') LIMIT 1[0m
[4;36;1mNews Columns (2.0ms)[0m [0;1mSHOW FIELDS FROM `news`[0m
Expired fragment: views/News_latest_fragment (0.0ms)
[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
Username : admin Role : Admin
[4;36;1mConfiguration Load (0.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'StudentAttendanceType') LIMIT 1[0m
[4;35;1mConfiguration Load (0.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'AvailableModules') [0m
[4;36;1mUser Load (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) [0m
[4;35;1mConfiguration Load (0.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'FirstTimeLoginEnable') LIMIT 1[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
[4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_value` = 'HR') LIMIT
1[0m
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
[4;35;1mprivileges_users Columns (3.0ms)[0m [0mSHOW FIELDS FROM
`privileges_users`[0m
[4;36;1mPrivilege Load (1.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 (0.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'PrecisionCount') LIMIT 1[0m
Rendering template within layouts/application
Rendering employee/add_rosterduty
ActionView::TemplateError (undefined method `full_name' for
nil:NilClass) on line #5 of app/views/employee/add_rosterduty.html.erb:
2: <%= show_header_icon %>
3: <h1><%= "Roster Duty" %></h1>
4: <div class='header-sep'>|</div>
5: <div class='sub-header'><%= @employee.full_name %>
6: <%= logger.info( "Employee: #{@employee.inspect}") %>
7: </div>
8:
model, method name is full_name which says full_name undefined.
Extracted source (around line #5):
Application Trace | Framework Trace | Full Trace
{"id"=>"2"}
Error messages are often a little tricky to understand but there is
often useful information there if the message is considered carefully.
Note that it is saying undefined method `full_name' for nil:NilClass.
This means that you have tried to call the method full_name on an
object that is nil. Looking at the code this means that @employee is
nil. You need to work out why that is so.
An easy way of doing simple debugging is to insert into your code lines
such as
logger.info( "Employee: #{@employee.inspect} )
That will insert a line into development.log showing the value of
@employee.
Colin
Dont know if I am right, but I tried this and the log is below:
<%= logger.info( "Employee: #{@employee.inspect}") %>
...
It is not necessary to post the complete log, only the bits around the failure.
ActionView::TemplateError (undefined method `full_name' for
nil:NilClass) on line #5 of app/views/employee/add_rosterduty.html.erb:
2: <%= show_header_icon %>
3: <h1><%= "Roster Duty" %></h1>
4: <div class='header-sep'>|</div>
5: <div class='sub-header'><%= @employee.full_name %>
6: <%= logger.info( "Employee: #{@employee.inspect}") %>
There is not much point putting the log line after the line that is
causing processing to stop due to the error. Also there is not much
point putting it anywhere in that bit of code as we already know (from
the error message) that it is nil. I expected that you would put it
in the area where you are setting up @employee to try to work out why
it is nil.
There is not much point putting the log line after the line that is
causing processing to stop due to the error. Also there is not much
point putting it anywhere in that bit of code as we already know (from
the error message) that it is nil. I expected that you would put it
in the area where you are setting up @employee to try to work out why
it is nil.
Colin
if u mean like the below, I did and the development log is the same
def add_rosterduty
@rosterduty = RosterDuty.new()
@employee = Employee.find(params[:id])
logger.info( "Employee: #{@employee.inspect}")
if request.post? and @rosterduty.save
flash[:notice] = t('flash7')
redirect_to :controller => "employee", :action => "add_rosterduty"
end
end
There is not much point putting the log line after the line that is
causing processing to stop due to the error. Also there is not much
point putting it anywhere in that bit of code as we already know (from
the error message) that it is nil. I expected that you would put it
in the area where you are setting up @employee to try to work out why
it is nil.
Colin
if u mean like the below, I did and the development log is the same
That line should insert a line into the log when it is executed. Are
you sure it is not there? Try
logger.info( "Employee: #{@employee.inspect}
*******************************************")
so that it will be easy to see. If it is still not there then that
suggests it is not executing that method at all. Are you sure you are
editting the right file. Do a global search in your editor for
add_rosterduty and make sure you have not defined it in more than one
place.
if request.post? and @rosterduty.save
flash[:notice] = t('flash7')
redirect_to :controller => "employee", :action => "add_rosterduty"
end
end
I don't see that it is related to this problem, but I notice from the
log that you are doing a GET on add_rosterduty when I think it should
be a post. Also it seems a bit odd that you make a new RosterDuty and
save it without setting any data in it. Again though, that is not the
cause of the problem you are seeing at the moment.
I don't see that it is related to this problem, but I notice from the
log that you are doing a GET on add_rosterduty when I think it should
be a post. Also it seems a bit odd that you make a new RosterDuty and
save it without setting any data in it. Again though, that is not the
cause of the problem you are seeing at the moment.
Colin
It also show error in form may be bcoz of this line
I don't see that it is related to this problem, but I notice from the
log that you are doing a GET on add_rosterduty when I think it should
be a post. Also it seems a bit odd that you make a new RosterDuty and
save it without setting any data in it. Again though, that is not the
cause of the problem you are seeing at the moment.
Colin
It also show error in form may be bcoz of this line
I suggest concentrating on one error at a time. Fix the other one
then move on to the next.
You have not told us the outcome of your tests from my previous post.
If it is definitely not showing the logger.info output then it is not
executing that code. This would be consistent with the fact that it
does not seem to be querying the database for your Employee.find line.
You need to work out why that is not the case.
You have not told us the outcome of your tests from my previous post.
If it is definitely not showing the logger.info output then it is not
executing that code. This would be consistent with the fact that it
does not seem to be querying the database for your Employee.find line.
You need to work out why that is not the case.
Colin
in the same employee cotroller this Employee.find works fine, I just
added this new method add_roterduty and here it is causing the error.
I note two undesirable aspects of the definition of add_rosterduty in
employee_controller.rb
1. You have declared it after the private statement, which makes it
not available as a controller action.
2. It is not even inside the class EmployeeController, it is just
stuck on the end (as are some other methods
Since there is no method EmployeeController#add_rosterduty but you
have added a route for it, rails has generated a default one for you,
which just renders the appropriate view, giving the error you see.
Finally, your controller is much too large and complex. I am not
going to wade through it trying to refactor it for you, but it is not
suitable for purpose as it is. Likely large amounts of the code can
be delegated to models, for example.
Finally, your controller is much too large and complex. I am not
going to wade through it trying to refactor it for you, but it is not
suitable for purpose as it is. Likely large amounts of the code can
be delegated to models, for example.
Colin
Thanks colin,
I moved the method up in controller but now it gives me the error:
NoMethodError in Employee#add_rosterduty
Showing app/views/employee/add_rosterduty.html.erb where line #14
raised:
undefined method `roster_duties_path' for #<ActionView::Base:0x19dd9fc0>
Extracted source (around line #14):