我在本地没有遇到错误,但在 Heroku 上。错误是:
'ActionView::Template::Error (nil:NilClass 的未定义方法 `each')'
'each' 指的是下面我的 User callback.html.erb 视图的 <% for i in @positions %> 行中 @position 中的每个 i:
<% for i in @positions %>
<strong>
<% begin %>
<%= @user.positions.find_by_id(i).title + " at " %>
<% rescue %>
<% end %>
<% begin %>
<%= @user.positions.find_by_id(i).company %>
<% rescue %>
<% end %>
</strong>
这是我的身份验证控制器的相关部分(回调部分)
def callback
...
@user = current_user
...
#positions
for i in 0..(positions.count-1)
begin
@company_i = companies[i]['name']
rescue
end
begin
@title_i = positions[i]['title']
rescue
end
begin
@industry_i = companies[i]['industry']
rescue
end
begin
@start_month_i = positions[i]['start-date']['month']
@start_year_i = positions[i]['start-date']['year']
rescue
end
begin
@end_month_i = positions[i]['end-date']['month']
@end_year_i = positions[i]['end-date']['year']
rescue
end
begin
@li_pos_id_i = positions[i]['id']
rescue
end
if Position.find_by_li_pos_id(@li_pos_id_i).nil?
@user.positions.build(li_pos_id: @li_pos_id_i, company: @company_i, title: @title_i,
industry: @industry_i, start_month: @start_month_i, start_year: @start_year_i,
end_month: @end_month_i, end_year: @end_year_i)
end
end
@user.save
@positions = @user.positions.map(&:id)
end
我认为这与我的 .find_by 方法返回 nil 值有关,但我不确定如何解决它。谢谢!
编辑授权控制器:
positions.each do |position|
begin
@li_pos_id = position.id
@title = position.title
@company = position.company.name
@industry = position.company.industry
@start_month = position.start_date.month
@start_year = position.start_date.year
@end_month = position.end_date.month
@end_year = position.end_date.year
rescue
end
unless Position.find_by_li_pos_id(@li_pos_id)
current_user.positions.build(li_pos_id: @li_pos_id, title: @title, company: @company, industry: @industry,
start_month: @start_month, start_year: @start_year, end_month: @end_month, end_year: @end_year)
end
@user.save
@user.positions.save
end