Ruby on Rails,未定义的方法`name' for nil:删除关联时的NilClass

时间:2016-01-29 06:51:52

标签: ruby-on-rails-3 simple-form cocoon-gem

我正在使用Rails 3.2.13 我有3个实体

class Mother < ActiveRecord::Base
    attr_accessible :previous_births_attributes
    belongs_to :participant
    has_many :previous_births
    accepts_nested_attributes_for :previous_births, :reject_if => :all_blank, allow_destroy: true
end

PreviousBirth

class PreviousBirth < ActiveRecord::Base
  attr_accessible :gender
  belongs_to :mother
end    

我使用cocoon显示我的视图

_form.html.erb

<div id='previous-births'>
  <%= f.simple_fields_for :previous_births do |task| %>
      <%= render 'previous_birth_fields', :f => task %>
  <% end %>
  <%= link_to_add_association 'add previous birth', f, :previous_births, class: 'btn btn-success btn-mini ' %>      
</div>

_previous_birth_fields.html.erb

<div class="nested-fields row">
  <table class="table table-compact span12">
    <thead>
     <tr>
       <th class="span1">Gender</th>
       <th class="span1">&nbsp;</th>
     </tr>
  </thead>
  <tbody>
    <tr>
      <td><%= f.input_field :gender, collection: ["Female", "Male"], :class => "span1" %></td>
      <td><%= link_to_remove_association "remove", f, class: 'btn btn-danger btn-small span1' %></td>
    </tr>
  </tbody>

  

添加previous_birth就像魅力一样。但是当我在更新期间删除关联时,我得到了

  

NoMethodError(未定义的方法name' for nil:NilClass): app/controllers/mothers_controller.rb:71:in阻止更新&#39;   app / controllers / mothers_controller.rb:70:在`update&#39;

mothers_controllers.rb

def update
  @mother = Mother.find(params[:id])
  participant = @mother.participant
  authorize participant
  respond_to do |format|
  if @mother.update_attributes(params[:mother]) #line 71
    format.html { redirect_to @mother.participant, notice: 'Mother was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @mother.errors, status: :unprocessable_entity }
  end
end

我母亲或以前的出生实体都没有名为name的属性。我试着谷歌,但没有发现任何东西。有没有人有任何想法?

谢谢

0 个答案:

没有答案