0

我的应用程序有错误。我有一个嵌套的 from,它在新页面中完美呈现,但不是在我想查看所有用户内容的页面中。这是错误:

NoMethodError in Resumes#index

Showing /Users/antonella/Desktop/resumin/resume/app/views/resumes/index.html.erb where line #10 raised:

undefined method `educations' for nil:NilClass

Extracted source (around line #10):

7:          <%= resume.personal_detail.address %>
8:          <%= resume.personal_detail.telephone %>
9:      </li>
10:         <% @resume.educations.each do |education| %>
11:         <li>
12:             <%= resume.education.school_name %>
13:             <%= resume.education.city %>

Rails.root: /Users/antonella/Desktop/resumin/resume

简历.rb

class Resume < ActiveRecord::Base
    has_one :personal_detail
    has_many :educations, :dependent => :destroy
    accepts_nested_attributes_for :personal_detail
    attr_accessible :personal_detail_attributes 
    validates_associated :personal_detail 

    accepts_nested_attributes_for :educations, :reject_if => lambda { |a| a[:education].blank? }, :allow_destroy => true
    attr_accessible :education_attributes
    validates_associated :education
end

resumes_controller.rb

def index 
    @resumes = Resume.all
  end

教育.rb

class Education &lsaquo; ActiveRecord::Base
    belongs_to :resume
    attr_accessible :school_name, :degree, :dates_attended, :type_of_study, :field_of_study, :city, :country
end

索引.html

<h1>All resumes</h1>
<ul>
    <% @resumes.each do |resume| %>
        <li>
            <%= resume.personal_detail.firstname %>
            <%= resume.personal_detail.lastname %>
            <%= resume.personal_detail.address %>
            <%= resume.personal_detail.telephone %>
        </li>
        <% @resume.educations.each do |education| %>
        <li>
            <%= resume.education.school_name %>
            <%= resume.education.city %>
            <%= resume.education.country %>
            <%= resume.education.dates_attended %>
            <%= resume.education.degree %>
            <%= resume.education.type_of_study %>
            <%= resume.education.field_of_study %>
        </li>
        <% end %>
    <% end %>
</ul>
4

1 回答 1

0

代替:

<% @resume.educations.each do |education| %>

它应该是:

<% resume.educations.each do |education| %>

请注意,该变量@resume不存在。你只有@resumes并且在你有的循环内resume

您还必须更改此部分:

<li>
  <%= resume.education.school_name %>
  <%= resume.education.city %>
  <%= resume.education.country %>
  <%= resume.education.dates_attended %>
  <%= resume.education.degree %>
  <%= resume.education.type_of_study %>
  <%= resume.education.field_of_study %>
</li>

到:

<li>
  <%= education.school_name %>
  <%= education.city %>
  <%= education.country %>
  <%= education.dates_attended %>
  <%= education.degree %>
  <%= education.type_of_study %>
  <%= education.field_of_study %>
</li>
于 2012-05-21T09:50:11.013 回答