【问题标题】:Deeply nested routes in Rails 3Rails 3 中深度嵌套的路由
【发布时间】:2011-04-20 18:11:58
【问题描述】:

我的 show.html.erb 中有这个:

<%= link_to "Pay on this contract", new_product_contract_payment_path %>

这个在routes.rb

  resources :products do
    resources :contracts do
      resources :payments 
    end
  end 

但是当我查看合同页面时,我得到了这个错误:

No route matches {:action=&gt;"new", :controller=&gt;"payments"}

当我使用rake routes 并且付款控制器在那里时,该路线就会出现。这是我在支付控制器中的新方法。

 def new
    @contract = Contract.find(params[:contract])
    @payment = @contract.line_items.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @payment }
    end
  end

对引发错误的原因有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    你需要传入一份合同和一个产品,比如:

     new_product_contract_payment_path(@product, @contract) 
    

    【讨论】:

    • 我应该提到我确实尝试过并收到此错误:No route matches {:action=&gt;"new", :controller=&gt;"payments", :product_id=&gt;nil, :contract_id=&gt;#&lt;Contract id: 17, product_id: 4, author_id: 1, begins: "2011-01-14", ends: "2012-01-14", created_at: "2011-01-14 01:49:02", updated_at: "2011-01-14 01:49:02"&gt;}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多