2017-11-11 46 views
0

我正在嘗試使三層嵌套模型工作。三層嵌套#show中的NoMethodError

試圖簡單地說:頭部模型有許多尾部,而這些尾部又有許多圖紙。

的形式來添加數據(圖紙)的第三層是給我的錯誤:

「NoMethodError在頭#節目」上_form.html.erb的第二行如下所示。

我試圖按照第二個巢中第一個巢的語法,但我猜測它是不正確的。

元首查看:show.html.erb:

<h2><%= @head.head_number %></h2> 
    <p><%= @head.head_description %></p> 
    <p><%= @head.inventory_code %></p> 
    <p><%= @head.category %></p> 
    <p><%= @head.life_cycle %></p> 
    <hr> 
    <%= link_to "Edit", edit_head_path(@head), :class => 'btn btn-default'%> 
    <hr> 
    <%= render 'tails/tails' %> 
    <%= render 'tails/form' %> 

尾巴視圖:_tails.html.erb:

<h3>Tail Numbers</h3> 
    <% @head.tails.each do |tail| %> 
     <div class="well"> 
     <p><strong><%= tail.tail_number %></strong> <%= tail.tail_description %> 
     <%= button_to 'Destroy Tail', [tail.head, tail], method: :delete, data: { confirm: 'Are you sure?' }%> 
     </p> 
     <%= render 'drawings/drawings' %> 
     <%= render 'drawings/form' %> 
     </div> 
    <% end %> 

附圖視圖:_form.html.erb:

<h5>Add Drawing</h5> 
    <%= form_for([@head.tail, @head.tail.drawings.build], :html => {:multipart => true}) do |f| %> 
     <p> 
     <%= f.label :dwg_rev %><br> 
     <%= f.text_field(:dwg_rev, {:class => 'form-control'}) %> 
     </p> 
     <p> 
     <%= f.submit({:class => 'btn btn-primary'}) %> 
     </p> 
    <% end %> 

的routes.rb

Rails.application.routes.draw do 

     root 'heads#index', as: 'home' 

     resources :heads do 
      resources :tails do 
       resources :drawings 
      end 
     end 

     resources :tails do 
      resources :drawings 
     end 

    end 

頭部模型 - head.rb

class Head < ApplicationRecord 

     has_many :tails, :dependent => :destroy 

    end 

尾模型 - tail.rb

class Tail < ApplicationRecord 

     belongs_to :head 
     has_many :drawings 

    end 

繪製模型 - drawing.rb

class Drawing < ApplicationRecord 

     belongs_to :tail 

    end 

頭控制器 - heads_controller.rb

class HeadsController < ApplicationController 

     def index 
     @heads = Head.order(:head_number) 
     end 

     def show 
     @head = Head.find(params[:id]) 
     end 

     def new 
     @head = Head.new 
     end 

     def create 
     @head = Head.new(head_params) 
     if(@head.save) 
      redirect_to @head 
     else 
      render 'new' 
     end 
     end 

     def edit 
     @head = Head.find(params[:id]) 
     end 

     def update 
     @head = Head.find(params[:id]) 
     if(@head.update(head_params)) 
      redirect_to @head 
     else 
      render 'edit' 
     end 
     end 

     private def head_params 
     params.require(:head).permit(:head_number, :head_description, :inventory_code, :category, :life_cycle) 
     end 
    end 

尾巴控制器 - tails_controller.rb:

class TailsController < ApplicationController 
     def create 
     @head = Head.find(params[:head_id]) 
     @tail = @head.tails.create(tail_params) 
     redirect_to head_path(@head) 
     end 

     def destroy 
     @head = Head.find(params[:head_id]) 
     @tail = @head.tails.find(params[:id]) 
     @tail.destroy 
     redirect_to head_path(@head) 
     end 

     private def tail_params 
     params.require(:tail).permit(:tail_number, :tail_description) 
     end 
    end 

圖紙控制器 - drawings_controller.rb:

class DrawingsController < ApplicationController 

    def create 
     @tail = Tail.find(params[:tail_id]) 
     @drawing = @tail.drawings.create(drawing_params) 
     redirect_to head_path(@head) 
    end 

    private def drawing_params 
     params.require(:drawing).permit(:dwg_rev) 
    end 

    end 

感謝您的幫助!

回答

0

@head沒有#tail,只有#tails

#_tails.html.erb 
<%= render 'drawings/form', tail: tail %> 

#_form.html.erb 
<%= form_for([tail, tail.drawings.build], :html => {:multipart => true}) do |f| %>