三级嵌套的#show中的NoMethodError

时间:2017-11-11 00:24:43

标签: ruby-on-rails ruby

我正在尝试使三级嵌套模型工作。

试着简单地说:头部模型有许多尾部,而这些尾部又有许多图纸。

添加第三级数据(图纸)的表单给了我错误:

" Heads中的NoMethodError#show"在_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 - 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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

school=0 for (i in 1:nrow(cleaned_us_news)){ if(Instructional.expenditure.per.student[i] > Out.of.state.tuition[i]){ school = school + 1} } 没有@head,只有#tail

#tails