Ruby on rails 未定义的方法';id';对于nil:创建操作上的NilClass

Ruby on rails 未定义的方法';id';对于nil:创建操作上的NilClass,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我是RubyonRails新手,我正在尝试创建一个带有标题和详细信息的程序,但在开发过程中遇到了这个错误。我的目标是创建一个标题,然后将其id保存在AREL的详细信息表中。 如有任何建议,将不胜感激。提前谢谢 EcnDetails中的命名错误#创建 显示C:/Ruby24-x64/code/ECN/app/views/ECN_details/new.html.erb,其中第1行出现: nil:NilClass的未定义方法“id” 提取的源(第1行附近): 错误详细信息: Rails.root:

我是RubyonRails新手,我正在尝试创建一个带有标题和详细信息的程序,但在开发过程中遇到了这个错误。我的目标是创建一个标题,然后将其id保存在AREL的详细信息表中。 如有任何建议,将不胜感激。提前谢谢

EcnDetails中的命名错误#创建 显示C:/Ruby24-x64/code/ECN/app/views/ECN_details/new.html.erb,其中第1行出现: nil:NilClass的未定义方法“id” 提取的源(第1行附近):

错误详细信息:

Rails.root: C:/Ruby24-x64/code/ECN
Application Trace | Framework Trace | Full Trace 
app/views/ecn_details/new.html.erb:1:in `_app_views_ecn_details_new_html_erb__19924186_93249280'
app/controllers/ecn_details_controller.rb:33:in `create'
Request
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"8WPqjsZJzmqnyOVvuQtKLMhas503UZqiB8jEUzvthtoCCa8k9Z7Mr+iX6y/Yt3Ig5lvNO4qTO5bbtKTsZZs5sw==",
 "ecn_detail"=>
  {"ecn_headers_id"=>"1",
   "old_parts_itemcode"=>"OD9008400A",
   "old_parts_partname"=>"FILTER BKT",
   "old_parts_instruction"=>"BUY",
   "new_parts_itemcode"=>"0EU026000",
   "new_parts_partname"=>"FILTER BASE BKT",
   "new_parts_instruction"=>"NO CHANGE UNTIL STOCKS DEPLETED",
   "remarks"=>"-",
   "bom"=>"MASTER BOM AS IS, 10-11-17",
   "sales"=>"-",
   "scm"=>"-",
   "overall_status"=>"WAITING FOR SCM UPDATE"},
 "commit"=>"Create Details",
查看代码:

<%= link_to("<< Back to List", ecn_details_path(:ecn_headers_id => @header.id), :class => 'back-link') %>

   <div class="ecn_details new">   <h2>Create ECN Details</h2>

   <%= form_for(@detail, :url => ecn_details_path(:ecn_headers_id => @header.id)) do |f| %>
@header.id),:class=>“反向链接”)%>
创建ECN详细信息
ecn_详细信息_路径(:ecn_headers_id=>@header.id))do | f |%>
控制器代码:

class EcnDetailsController < ApplicationController
  before_action :find_header, :only => [:index, :new]    

  def index
    #@details = @header.details.sorted
    @details = EcnDetail.where(:ecn_headers_id => @header.id).sorted
    rescue ActiveRecord::RecordNotFound => handle_record_not_found
    #@page_title = "Details"
  end

  def show
    @details = EcnDetail.find(params[:id])
  end
  def new
    @detail = EcnDetail.new(:ecn_headers_id => @header.id)
  end

  def create 
    @detail = EcnDetail.new(detail_params)
    if @detail.save 
      flash[:notice] = "ECN Details created successfully."
      redirect_to(ecn_details_path(:ecn_headers_id => @header.id))
    else
      flash[:error] = "Error in creating ECN Details."
      render('new')
    end
  end

  def edit
    @details = EcnDetail.find(params[:id])
  end

  def update
    @details = EcnDetail.find(params[:id])
    if @details.update_attributes(details_params)
     # if save succeeds
      flash[:notice] = "ECN Details updated successfully."
      redirect_to(ecn_details_path(@detail, :ecn_header_id => @header.id))
    else
      # if save fails
      flash[:error] = "Failed to update ECN Details"
      render('edit')
    end
  end

  def delete
    @details = EcnDetail.find(params[:id])
  end

  def destroy
    @detail = Detail.find(params[:id])
    @page.destroy
    flash[:notice] = "ECN Details destroyed successfully."
    redirect_to(details_path(:ecn_header_id => @header.id))
  end

  private
  def detail_params
    params.require(:ecn_detail).permit(:ecn_headers_id,:old_parts_itemcode,
    :old_parts_partname, :old_parts_instruction, :new_parts_itemcode,
    :new_parts_partname, :new_parts_instruction, :remarks, :bom, :sales,
    :scm, :overall_status)
  end

  def find_header
    @header = EcnHeader.find(params[:ecn_headers_id])
  end

  def find_headers
    @header = EcnHeader.sorted
  end

  def handle_record_not_found
    yield
    redirect_to ecn_details_path, :flash => { :notice => "No Record found."}
  end
end
class EcnDetailsController[:index,:new]
def索引
#@详细信息=@header.details.sorted
@details=EcnDetail.where(:ecn\u headers\u id=>@header.id)。已排序
rescue ActiveRecord::RecordNotFound=>处理未找到的记录
#@page_title=“详细信息”
结束
def秀
@details=EcnDetail.find(参数[:id])
结束
def新
@detail=EcnDetail.new(:ecn\u headers\u id=>@header.id)
结束
def创建
@细节=EcnDetail.new(细节参数)
if@detail.save
flash[:notice]=“已成功创建ECN详细信息。”
重定向到(ecn\u详细信息\u路径(:ecn\u headers\u id=>@header.id))
其他的
flash[:error]=“创建ECN详细信息时出错。”
呈现('新')
结束
结束
定义编辑
@details=EcnDetail.find(参数[:id])
结束
def更新
@details=EcnDetail.find(参数[:id])
如果@details.update_属性(details_参数)
#如果保存成功
flash[:notice]=“ECN详细信息已成功更新。”
重定向到(ecn\u details\u路径(@detail,:ecn\u header\u id=>@header.id))
其他的
#如果保存失败
闪存[:错误]=“未能更新ECN详细信息”
渲染('编辑')
结束
结束
def删除
@details=EcnDetail.find(参数[:id])
结束
def销毁
@detail=detail.find(参数[:id])
@毁灭
flash[:notice]=“ECN详细信息已成功销毁。”
重定向到(详细信息路径(:ecn\u header\u id=>@header.id))
结束
私有的
def detail_参数
参数require(:ecn_detail).permit(:ecn_headers_id,:old_parts_itemcode,
:old_parts_partname、:old_parts_指令、:new_parts_itemcode、,
:新零件名称,:新零件说明,:备注,:物料清单,:销售,
:scm,:总体状态)
结束
def查找头
@header=EcnHeader.find(参数[:ecn\u headers\u id])
结束
def find_头
@header=EcnHeader.sorted
结束
未找到def句柄记录
产量
重定向到ecn\u详细信息\u路径:flash=>{:notice=>“未找到记录”。}
结束
结束
视图:


@header.id),:class=>backlink')%>
创建ECN详细信息
ecn_详细信息_路径(:ecn_headers_id=>@header.id))do | f |%>
'form',:locals=>{:f=>f})%>

-局部视图

<%= error_messages_for(f.object) %>

<table summary="ECN Details form fields">
   <tr>
   <%= f.hidden_field(:ecn_headers_id)%>
     <th>OLD ITEM CODE</th>
     <td><%= f.text_field(:old_parts_itemcode) %></td>
   </tr>
   <tr>
     <th>OLD PART NAME</th>
     <td><%= f.text_field(:old_parts_partname) %></td>
   </tr>
   <tr>
     <th>INSTRUCTION</th>
     <td><%= f.text_field(:old_parts_instruction) %></td>
   </tr>
   <tr>
     <th>NEW ITEM CODE</th>
     <td><%= f.text_field(:new_parts_itemcode) %></td>
   </tr>
   <tr>
     <th>NEW PART NAME</th>
     <td><%= f.text_field(:new_parts_partname) %></td>
   </tr>
   <tr>
     <th>INSTRUCTION</th>
     <td><%= f.text_field(:new_parts_instruction) %></td>
   </tr>
   <tr>
     <th>REMARKS</th>
     <td><%= f.text_field(:remarks) %></td>
   </tr>
   <tr>
     <th>BOM</th>
     <td><%= f.text_field(:bom) %></td>
   </tr>
   <tr>
     <th>SALES</th>
     <td><%= f.text_field(:sales) %></td>
   </tr>
   <tr>
     <th>SCM</th>
     <td><%= f.text_field(:scm) %></td>
   </tr>
   <tr>
     <th>STATUS</th>
     <td><%= f.text_field(:overall_status) %></td>
   </tr>
</table>

旧项目代码
旧零件名称
指示
新项目代码
新零件名称
指示
评论
物料清单
出售
供应链管理
地位
错误消息“undefined method for nil:NilClass”表示您正试图调用某个方法,该方法不希望为nil,但实际为。如果您查看
new.html.erb
中的类似#1,它会显示:
@header.id
,因此错误消息告诉您
@header
nil
或未设置

您编写创建操作的方式,如果无法保存
EcnDetail
它将呈现新视图。当您告诉rails渲染不同的视图时,它不会运行控制器代码,它只渲染erb文件

从您发布的控制器的代码外观来看,您正在为新操作设置
@header
。这不是在创建操作中设置的,因此当创建失败时,
@header
将被取消设置,rails将其视为
nil


如果你需要进一步的帮助,请发布您的全部控制器代码。

谢谢您的即时回复,我已经在上面更新了我的控制器。您好@Josh,我仍在检查'@header',但我可以看到它正确地抛出了我的详细操作中的数据,新基来自生成的sql查询,'@header'id作为ecn_headers_id出现在我的URL参数中。您能解释更多吗详细说明如何调试此错误。感谢您在保存Ecn详细信息或指向
new
操作时遇到此错误
<%= error_messages_for(f.object) %>

<table summary="ECN Details form fields">
   <tr>
   <%= f.hidden_field(:ecn_headers_id)%>
     <th>OLD ITEM CODE</th>
     <td><%= f.text_field(:old_parts_itemcode) %></td>
   </tr>
   <tr>
     <th>OLD PART NAME</th>
     <td><%= f.text_field(:old_parts_partname) %></td>
   </tr>
   <tr>
     <th>INSTRUCTION</th>
     <td><%= f.text_field(:old_parts_instruction) %></td>
   </tr>
   <tr>
     <th>NEW ITEM CODE</th>
     <td><%= f.text_field(:new_parts_itemcode) %></td>
   </tr>
   <tr>
     <th>NEW PART NAME</th>
     <td><%= f.text_field(:new_parts_partname) %></td>
   </tr>
   <tr>
     <th>INSTRUCTION</th>
     <td><%= f.text_field(:new_parts_instruction) %></td>
   </tr>
   <tr>
     <th>REMARKS</th>
     <td><%= f.text_field(:remarks) %></td>
   </tr>
   <tr>
     <th>BOM</th>
     <td><%= f.text_field(:bom) %></td>
   </tr>
   <tr>
     <th>SALES</th>
     <td><%= f.text_field(:sales) %></td>
   </tr>
   <tr>
     <th>SCM</th>
     <td><%= f.text_field(:scm) %></td>
   </tr>
   <tr>
     <th>STATUS</th>
     <td><%= f.text_field(:overall_status) %></td>
   </tr>
</table>