undefined 方法 'id' for nil:NilClass on Create action



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

错误图像EcnDetails中的NoMethodError #create显示 C:/Ruby24-x64/code/ECN/app/views/ecn_details/new.html.erb,其中 #1 行凸起:undefined 方法 'id' for nil:NilClass提取的源(围绕第 #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| %>

控制器代码:

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

视图:

<%= 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| %>
    <%= render(:partial => 'form', :locals => {:f => f})%>
    <div class="form-buttons">
        <%= f.submit("Create Details") %>
    </div>
<% end %>

-部分视图

<%= 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>
错误消息

"nil:NilClass 的未定义方法"意味着您正在尝试对不希望为 nil 但确实如此的东西调用方法。 如果您查看 #1 的 new.html.erb它说:@header.id所以错误消息告诉您@header正在nil或未设置。

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

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

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

最新更新