1

I am a newbie in ruby on rails it's also my first ruby application.

I want to show data from two tables these are

applied_jobs table: it contains the below columns:

applied_jobs_id | jobseeker_id | expected_salary

jobseekers table: it contains the below columns:

jobseeker_id | year_of_experience | name

My view file codes:

 <tbody>
    <% @applicants_view.each do | applicants | %>
      <tr>
        <td><%=  applicants.jobseeker.name%></td>
        <td><%=applicants.jobseeker.year_of_experience %></td>
        <td><%= applicants.expected_salary %></td>
       </tr>
     <% end %>
  </tbody>

My controller codes:

def applicantsList

   @applicants_view = AppliedJob.paginate(page: params[:page],

:per_page => 4).order('applied_jobs_id DESC')

end

My applied_job model codes:

class AppliedJob < ActiveRecord::Base

    belongs_to :jobseeker
end

My jobseeker model codes:

class Jobseeker < ActiveRecord::Base

    has_one :applied_job

end

When I try to show data it's showing the below error

 C:/wamp/www/hire_us/app/views/applicants_list/applicantsList.html.erb
   where line #51 raised:

  undefined method `name' for nil:NilClass

  Rails.root: C:/wamp/www/hire_us
  Application Trace | Framework Trace | Full Trace

  app/views/applicants_list/applicantsList.html.erb:51:in `block in
 _app_views_applicants_list_applicants_ist_html_erb___1439381291_141337560'

  app/views/applicants_list/applicantsList.html.erb:49:in
    '_app_views_applicants_list_applicants_ist_html_erb___1439381291_141337560'

Someone can help me, please to identify where the bug?

1
  • 1
    That means that particular appliedjob record has no associated jobseeker to it. Commented Jan 26, 2016 at 8:42

1 Answer 1

2

You can avoid the error by adding

<% @applicants_view.each do | applicants | %>
   <% next unless applicants.jobseeker %>
   ...
<% end %>

or

<% @applicants_view.each do | applicants | %>
   <% applicants.jobseeker.try(:name) %>
   ...
<% end %>
Sign up to request clarification or add additional context in comments.

5 Comments

At first, thanks for answer, but my issue is still @Ho Man
does the wamp stack automatically restart the server / reload the files?
didn't restart, but reloaded
applicants.jobseeker.try(:name). if it still fails on the same error, same line, its not reloading :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.