2016-01-15 42 views
-1

我有我的應用程序的‘未定義的方法’的問題,並沒有發現來自何方:(未定義的方法`設施的零:NilClass - 嵌套行動

在我的應用程序,我有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 

當我嘗試創建一個新的協議,以下錯誤消息彈出「未定義的方法`設施的零:NilClass「(在上面的交易控制器中以粗體顯示)。

我在做什麼錯了?

非常感謝,並有很好的週末:)

回答

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 
+0

非常感謝阿龍我的工作! –

+0

@EtienneR沒問題。如果這對你有幫助,你是否認爲答案已被接受?謝謝! –

相關問題