首页 > 解决方案 > 从 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

我究竟做错了什么?

标签: ruby-on-rails

解决方案


您的 redirect_to 路径不在您的路线中。你想去这customer_opportunity_sop_header_path条路,而这条路不在你的路线中。我猜你想导航到customer_opportunity_sop_header_sop_details_path.


推荐阅读