未定义的方法`设施' for nil:NilClass - 嵌套动作

时间:2016-01-15 15:21:50

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

我有一个"未定义的方法"我的应用程序问题,并且找不到它的来源:(。

在我的应用中,我有4个型号: 交易,池(属于交易),工厂(属于池),Facilityschedule(属于工厂)。

class Deal < ActiveRecord::Base
    has_many :pools, :dependent => :destroy
    accepts_nested_attributes_for :pools, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
    end

class Pool < ActiveRecord::Base
    belongs_to :deal
    has_many :facilities, :dependent => :destroy
    accepts_nested_attributes_for :facilities, :allow_destroy => true
end

class Facility < ActiveRecord::Base
    belongs_to :pool
    has_many :facilityschedules, :dependent => :destroy
    accepts_nested_attributes_for :facilityschedules, :reject_if => lambda { |a| a[:date].blank? }, :allow_destroy => true
end

class Facilityschedule < ActiveRecord::Base
    belongs_to :facility
end

我有一个部分表单,允许用户创建所有这些:

<%= form_for(@deal) do |f| %>
  <% if @deal.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@deal.errors.count, "error") %> prohibited this deal from being saved:</h2>

      <ul>
      <% @deal.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>



  <div class="field">
    <%= f.label :name, "Deal name"%>
    <%= f.text_field :name %>
    <br/>
  </div>


<div>
  <%= f.fields_for :pools do |builder|%>
  <%= builder.label :name, "Pool name" %>
  <%= builder.text_field :name, :rows => 3 %>
  <%= builder.check_box :_destroy %>
  <%= builder.label :_destroy, "Remove Pool" %>
  <br/>

      <%= builder.fields_for :facilities do |fbuilder|%>
      <%= fbuilder.label :name, "Facility name" %>
      <%= fbuilder.text_field :name, :rows => 3 %>
      <%= fbuilder.check_box :_destroy %>
      <%= fbuilder.label :_destroy, "Remove Facility" %>
      <br/>

          <%= fbuilder.fields_for :facilitieschedules do |sbuilder|%>
        <%= sbuilder.label :date, "Schedule" %>
        <%= sbuilder.text_field :date, :rows => 3 %>
        <%= sbuilder.check_box :_destroy %>
        <%= sbuilder.label :_destroy, "Remove Schedule" %>
        <br/>

</div>
        <% end %>
        <% end %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>


<% end %>

最后,我有问题所在的交易控制器(新动作):

class DealsController < ApplicationController
  before_action :set_deal, only: [:show, :edit, :update, :destroy]
  # GET /deals
  # GET /deals.json
  def index
    @deals = Deal.all
  end

  # GET /deals/1
  # GET /deals/1.json
  def show
  end

  # GET /deals/new
  def new
  @deal = Deal.new

      2.times do
          pool = @deal.pools.build
            2.times do
                **facility = @pool.facilities.build**
                1. times { facility.facilityschedules.build }
            end
      end
  end

  # GET /deals/1/edit
  def edit
  end

  # POST /deals
  # POST /deals.json
  def create
    @deal = Deal.new(deal_params)

    respond_to do |format|
      if @deal.save
        format.html { redirect_to @deal, notice: 'Deal was successfully created.' }
        format.json { render :show, status: :created, location: @deal }
      else
        format.html { render :new }
        format.json { render json: @deal.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /deals/1
  # PATCH/PUT /deals/1.json
  def update
    respond_to do |format|
      if @deal.update(deal_params)
        format.html { redirect_to @deal, notice: 'Deal was successfully updated.' }
        format.json { render :show, status: :ok, location: @deal }
      else
        format.html { render :edit }
        format.json { render json: @deal.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /deals/1
  # DELETE /deals/1.json
  def destroy
    @deal.destroy
    respond_to do |format|
      format.html { redirect_to deals_url, notice: 'Deal was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_deal
      @deal = Deal.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def deal_params
      params.require(:deal).permit(:name, pools_attributes: [:id, :name, :number, :deal_id, :_destroy, facilities_attributes: [:id, :name, :pool_id, :_destroy, facilityschedules_attributes: [:id, :facility_id, :date, :_destroy]]])
    end
end

当我尝试创建新交易时,弹出以下错误消息&#34;未定义的方法`设施&#39;为零:NilClass&#34; (在交易控制器中以粗体显示)。

我做错了什么?

非常感谢并有一个愉快的一周结束:)

1 个答案:

答案 0 :(得分:0)

# GET /deals/new
def new
@deal = Deal.new

  2.times do
      pool = @deal.pools.build
        2.times do
            **facility = @pool.facilities.build**
            1. times { facility.facilityschedules.build }
        end
  end
end

在上面的代码中,您从未设置@pool变量,而是设置池。

Nil :: NilClass的未定义方法绝不是没有声明方法,而是你试图在nil对象上调用它,这是NilClass的对象。

此方法中也存在一些奇怪的模式。我从来没有在Rails中看到过**代码。你想把这条线注释掉吗?

这会更好吗?

# GET /deals/new
def new
  @deal = Deal.new

  2.times do
    @pool = @deal.pools.build
    2.times do
      facility = @pool.facilities.build
      facility.facilityschedules.build
    end
  end
end