ruby-on-rails - 从 link_to 创建一条新记录会导致 urlgenerationerror
问题描述
当用户单击链接时,我正在尝试直接在表中创建记录。
但是,当它尝试加载视图时会生成以下错误。
ActionController::UrlGenerationError in SopHeaders#show
No route matches {:action=>"create", :company_id=>2, :controller=>"sop_detail", :cost=>0.1e2, :cost_sop=>0.103e2, :customer_id=>1, :desc1=>"Printer cable YDP30 - Secura 224 ICEU", :flag_required=>true, :id=>"4", :list_price=>0.0, :opportunity_id=>"9", :product_code=>"01-69Y03293", :quantity=>1, :sophead_id=>4, :unit_price=>0.0}
我的观点
<%= link_to 'Add to Quote', {:controller => "sop_details",
:action => "create",
:sophead_id => @sop_header.id,
:customer_id => @customer.id,
:company_id => @customer.company_id,
:product_code => stock.product_code,
:unit_price => stock.price,
:quantity => 1,
:cost => stock.cost,
:cost_sop => stock.cost_sop,
:list_price => stock.list_price,
:flag_required => true,
:desc1 => stock.desc1},
:method => "post" %>
我的控制器
def new
@sop_detail = SopDetail.new
end
def create
respond_to do |format|
if @sop_detail.save
format.html { redirect_to customer_opportunity_sop_header_path(@customer, @opportunity, @sop_header) }
format.json { render :show, status: :created, location: @sop_header }
else
format.html { render :new }
format.json { render json: @sop_header.errors, status: :unprocessable_entity }
end
end
end
我的路由表输出
customer_opportunity_sop_header_sop_details GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details(.:format) sop_details#index
POST /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details(.:format) sop_details#create
new_customer_opportunity_sop_header_sop_detail GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/new(.:format) sop_details#new
edit_customer_opportunity_sop_header_sop_detail GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id/edit(.:format) sop_details#edit
customer_opportunity_sop_header_sop_detail GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id(.:format) sop_details#show
PATCH /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id(.:format) sop_details#update
PUT /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id(.:format) sop_details#update
我究竟做错了什么?
解决方案
您的 redirect_to 路径不在您的路线中。你想去这customer_opportunity_sop_header_path
条路,而这条路不在你的路线中。我猜你想导航到customer_opportunity_sop_header_sop_details_path
.
推荐阅读
- java - 使字符串连续 x 没有所需的最小更改。A,然后在 (nx) 个 B 之后
- asp.net-core - c# asp.net core 3 从控制器调用不同的方法,取决于请求体
- c# - dotnet core 中的 AWS Lambda 似乎无法识别 Cloudwatch 事件常量值
- android - 在旧 Android 上更改 LayoutParams.height 不会完全改变大小
- flutter - 安装包存储不足错误
- node.js - 是否可以将“next/react + node/express”Web 应用程序部署到一个服务器帐户中,例如 heroku?
- javascript - 检查一个数组中的对象是否包含在另一个对象数组中
- r - 在检查因子水平和缺失数据后,`ggeffect` 或 `Effect` 无法估计模型中的边际效应
- visual-studio-code - VSCode 中的实时 SASS 编译器不起作用?
- spring - Spring批处理-如何根据条件检查添加Flow