我是Ruby on Rails的新手,我正在尝试使用标题和详细信息创建一个程序,并在开发时遇到此错误。我的目标是创建标题,然后将其ID保存在AREL的详细信息表中。 任何建议将不胜感激。提前致谢
error image EcnDetails中的NoMethodError #create 显示C:/Ruby24-x64/code/ECN/app/views/ecn_details/new.html.erb,其中第1行引发: nil的未定义方法`id':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>
答案 0 :(得分:1)
错误消息&#34; nil的未定义方法:NilClass&#34;意味着你试图在一些你不希望为零的东西上调用一种方法。如果您看起来像new.html.erb
的#1那么它会显示:@header.id
,因此错误消息告诉您@header
是nil
或未设置。
您编写创建操作的方式,如果它无法保存EcnDetail
,则会呈现新视图。当你告诉rails渲染一个不同的视图时,它不会运行控制器代码,它只是渲染erb文件。
根据您发布的控制器代码的外观,您可以在其他地方为新操作设置@header
。这不是在创建操作中设置的,因此当创建失败时@header
未设置且rails将其视为nil
如果您需要进一步的帮助,请发布您的整个控制器代码。