Calling method from model create error

Dear Experts/Friends

I created a model for and moved it to a sub folder in my model’s
directory.

Then wrote in the existing controller named employee the below code to
add roster duty.

def add_rosterduty()
@rosterduty = RosterDuty.new()
@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.

******** add_rosterduty.html.erb ***********

<%=make_breadcrumb%> <%=render_breadcrumbs%>

<% form_for(@rosterduty) do |a| %>
<%= error_messages_for :rosterduty %>

  <div id="necessary-field-notification"> <%= t('legend1') %> <span

class=“necessary-field”>* <%= t(‘legend2’) %>


<%= t(‘general_details’) %>




<%= a.hidden_field :employee_id, :value => params([:id]) %>



  <div class="label-field-pair">
    <label for="employee_name"><%= "Duty name:" %></label>
    <div class="text-input-bg"><%= a.text_field :duty_name %></div>
  </div>
  <hr class="label-underline"></hr>
<div class="label-field-pair">
    <label for="employee_name"><%= "Duty place:" %></label>
    <div class="text-input-bg"><%= a.text_field :duty_place %></div>
  </div>
  <hr class="label-underline"></hr>
<div class="label-field-pair">
    <label for="employee_name"><%= "Day first:" %></label>
    <div class="text-input-bg"><%= a.text_field :duty_day1 %></div>
  </div>
  <hr class="label-underline"></hr>
<div class="label-field-pair">
    <label for="employee_name"><%= "Day last:" %></label>
    <div class="text-input-bg"><%= a.text_field :duty_day2 %></div>
  </div>
  <hr class="label-underline"></hr>
<div class="label-field-pair">
    <label for="employee_name"><%= "Day time:" %></label>
    <div class="text-input-bg"><%= a.text_field :duty_time %></div>
  </div>
  <hr class="label-underline"></hr>
<div class="label-field-pair">
    <label for="employee_name"><%= "Employee id:" %></label>
    <div class="text-input-bg"><%= a.text_field :employee_id

%>




<%= submit_tag “► #{t(‘save_and_proceed’)}”, :class =>
“submit_button”, :disable_with => “► #{t(‘please_wait’)}” %>
<% end %>

******** Method in employee model ********
def full_name
“#{first_name} #{middle_name} #{last_name}”
end

Pls help, thanks in advance.

On 6 March 2016 at 08:00, Naveed A. [email protected] wrote:

@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

Colin L. wrote in post #1181947:

On 6 March 2016 at 08:00, Naveed A. [email protected] wrote:

@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):

2: <%= show_header_icon %>
3:

<%= “Roster Duty” %>


4:
|

5:
<%= @employee.full_name %>

6:
7:
8:

RAILS_ROOT: D:/Ansi/fed343_new

Application Trace | Framework Trace | Full Trace
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in
method_missing' D:/Ansi/fed343_new/app/views/employee/add_rosterduty.html.erb:5:in_run_erb_app47views47employee47add_rosterduty46html46erb’
D:/Ansi/fed343_new/app/controllers/application_controller.rb:361:in
`render’
Request

Parameters:

{“id”=>“2”}

On 6 March 2016 at 09:14, Naveed A. [email protected] wrote:

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

Colin L. wrote in post #1181949:

On 6 March 2016 at 09:14, Naveed A. [email protected] wrote:

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:

<%= “Roster Duty” %>


4:
|

5:
<%= @employee.full_name %>
6: <%= logger.info( “Employee: #{@employee.inspect}”) %>
7:

8:
app/views/employee/add_rosterduty.html.erb:5
app/controllers/application_controller.rb:361:in `render'

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

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

On 6 March 2016 at 11:08, Naveed A. [email protected] wrote:

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}”)

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.

Colin

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

my link is like this: http://localhost:7524/employee/add_rosterduty/2

<%= a.hidden_field :employee_id, :value => params[:id] %>

Error is:

Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id
Extracted source (around line #14):

11: <%=render_breadcrumbs%>
12:
13:
14: <% form_for(@rosterduty) do |a| %>
15: <%= error_messages_for :rosterduty %>
16:


17:

On 6 March 2016 at 09:52, Naveed A. [email protected] wrote:

object that is nil. Looking at the code this means that @employee is
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:

<%= “Roster Duty” %>


4:
|

5:
<%= @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.

Colin

On 6 March 2016 at 12:32, Naveed A. [email protected] wrote:

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.

Colin

I suggest concentrating on one error at a time. Fix the other one
then move on to the next.

Colin

I just wanted to tell you this bcoz may it gives u any idea about the
error.

On 7 March 2016 at 06:31, Naveed A. [email protected] wrote:

I suggest concentrating on one error at a time. Fix the other one
then move on to the next.

Colin

I just wanted to tell you this bcoz may it gives u any idea about the
error.

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

On 7 March 2016 at 11:52, Naveed A. [email protected] wrote:

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.

If the controller is short then post it here, otherwise put is
somewhere like pastebin [1] so we can see it.

Also put the results of the log since adding the logger.info line there.

Colin

[1] https://help.ubuntu.com/community/Pastebinit

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.

On 7 March 2016 at 12:32, Naveed A. [email protected] wrote:

there is,

employee controller
employee model
roster_duty model
and add_rosterduty.html.erb files and the development log file

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.

Colin

If the controller is short then post it here, otherwise put is
somewhere like pastebin [1] so we can see it.

Also put the results of the log since adding the logger.info line there.

Colin

I the link http://ansicollege.net/dummy/

there is,

employee controller
employee model
roster_duty model
and add_rosterduty.html.erb files and the development log file

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):

11: <%=render_breadcrumbs%>
12:
13:
14: <% form_for(@rosterduties) do |a| %>
15: <%= a.error_messages%>
16:

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):

11: <%=render_breadcrumbs%>
12:
13:
14: <% form_for(@rosterduties) do |a| %>
15: <%= a.error_messages%>
16:

Finally it worked, added map.rosources roster_duties routes

Thanks again Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs