2017-11-11 6 views
0

3レベルのネストされたモデルを作成しようとしています。3レベルネストの#showのNoMethodError

簡単に言えば、頭のモデルには多くの尾があり、多くの尾には多くの図があります。データの第3のレベル(図面)を追加する

形態は私にエラーを与えている:

以下に示す_form.html.erbの2行目の「ヘッド位ショーでNoMethodErrorを」。

私は2番目のネストで最初のネストの構文に従おうとしていましたが、それは正しくないと推測しています。

ヘッドビュー: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| %>