Ruby on Rails Sunday, March 6, 2016

On 6 March 2016 at 09:14, Naveed Alam <lists@ruby-forum.com> wrote:
> Colin Law wrote in post #1181947:
>> On 6 March 2016 at 08:00, Naveed Alam <lists@ruby-forum.com> 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: <h1><%= "Roster Duty" %></h1>
> 4: <div class='header-sep'>|</div>
> 5: <div class='sub-header'><%= @employee.full_name %></div>
> 6:
> 7: </div>
> 8: <div id="page-yield">
> 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"}


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

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLu7owvTpg%2B-nBaQTMi_LL56dK1%2Bchnoi%2ByQao7WCBCZig%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment