2017-11-11 45 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| %>