从link_to创建新记录会导致urlgenerationerror

时间:2018-08-13 11:25:23

标签: ruby-on-rails

当用户单击链接时,我试图直接在表中创建记录。

但是,当它尝试加载视图时却产生了以下错误。

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

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您的redirect_to路径不在您的路由中。您要转到customer_opportunity_sop_header_path路径,但该路径不在您的路线中。我猜您想导航到customer_opportunity_sop_header_sop_details_path