Rails - 嵌套循环不起作用

时间:2016-03-02 04:58:51

标签: ruby-on-rails ruby ruby-on-rails-4 nested-loops

每个主题都有很多页面,每个页面都有很多版本。在该主题的所有者的节目主题页面上,我希望用户能够更新该页面上的所有页面和版本。但是,我收到了这个错误:

undefined method `versions' for nil:NilClass

我的展示页面:

%h1
  = @subject.title
%h3 Subject Synopsis
%p
  = @subject.synopsis

- @subject.pages.each do |p|
    %h6
      = p.number
      = ": " + p.title
    %p
      = p.synopsis
    %table
      %tbody
        - p.versions.each do |v|
          %tr
           %td v.number
           %td v.create_at
    = render 'versions/form'

版本形式:

= simple_form_for [@page, @page.versions.build] do |f|
  = f.input :number, placeholder: 'Number', label: false
  = f.text_area :content, placeholder: 'Content', label: false
  = f.button :submit

2 个答案:

答案 0 :(得分:2)

- @subject.pages.each do |p|
        %h6
          = p.number
          = ": " + p.title
        %p
          = p.synopsis
        %table
          %tbody
            - p.versions.each do |v|

答案 1 :(得分:0)

I wasn't passing through the loop variable. Answer is:

= render 'versions/form', p: p


= simple_form_for [p, p.versions.build] do |f|