如何为路径

时间:2017-01-27 19:30:28

标签: ruby-on-rails ruby view model controller

我有3个型号。项目有许多订单项,订单行有很多包。我有一个使用project_orderline作为Show方法的视图,我将所有包呈现为部分。它工作正常,但是当我添加一个新包时,我想在create action中重定向(实际重定向)相同的视图。

  class PackagesController < ApplicationController
  before_action :set_package, only: [:show, :edit, :update, :destroy]

  def create
    project = Project.find(params[:project_id])
    orderline = project.orderlines.find(params[:orderline_id])
    @package = orderline.packages.new(package_params)

      if @package.save
        flash[:success] = "Package was successfully created."
        redirect_to project_orderline_path(orderline)
      else
        render 'new'
      end
  end

但我有以下错误:

ActionController::UrlGenerationError in PackagesController#create

我该如何解决这个问题?

PS。:所以,这是文本格式的错误消息:

PackageController中的ActionController :: UrlGenerationError #create 没有路线匹配{:action =&gt;&#34; show&#34;,:controller =&gt;&#34; orderlines&#34;,:id =&gt; nil,:orderline_id =&gt;&#34; 18&# 34;,:project_id =&gt;#,origin:&#34; 123&#34;,project_id:48,created_at:&#34; 2017-01-27 18:36:59&#34;,updated_at:&#34 ; 2017-01-27 18:36:59&#34;,小计:#&gt;}缺少必需的密钥:[:id]

路线:

resources :projects do
  resources :orderlines do
     resources :packages
   end
end

1 个答案:

答案 0 :(得分:1)

我不确定为什么你的project_orderline_path(orderline)无效,但既然你想回去,你可以这样做:

redirect_to :back

也许您可以发布您的路线,并将错误作为问题中的文字而不是屏幕截图。