[英]Issue to show data from relational database tables in rails
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表:它包含以下各列:
applied_jobs_id |
Applied_jobs_id | jobseeker_id |
jobseeker_id | expected_salary
期望薪水
jobseekers table: it contains the below columns: Jobseekers表:它包含以下各列:
jobseeker_id |
jobseeker_id | year_of_experience |
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') :per_page => 4).order('applied_jobs_id DESC')
end
My applied_job model codes: 我的Applied_job模型代码:
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? 有人可以帮助我,请找出错误所在?
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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.