gpt4 book ai didi

ruby-on-rails-3 - Rails 嵌套路由,更新 Redirect_To 问题

转载 作者:行者123 更新时间:2023-12-01 08:01:19 25 4
gpt4 key购买 nike

我试图在更新发票后让我的路由正常工作。

我的 routes.rb 中有一个包含许多发票和以下内容的订单

resources :orders do
resources :invoices
end

resources :invoices

在我的发票 Controller 中:

 def update
@invoice = Invoice.find(params[:id])
respond_to do |format|
if @invoice.update_attributes(params[:invoice])
format.html { redirect_to(invoice_path(@invoice), :notice => 'Invoice was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end

我可以通过以下方式创建发票:

/orders/1/invoices/new

但是当我保存或更新时,我被发送到:

/invoices/1

我需要重定向回订单发票路径:

/orders/1/invoices/1

还尝试将重定向更改为:

   redirect_to(order_invoice_path(@order, @invoice), :notice => 'Invoice was successfully updated.')

这行得通,但它会将我发送到错误的 url,其中订单 ID 与发票 ID 相同...

感谢任何帮助。

-- 更新--

如果我在我的发票 Controller 中尝试以下操作,我也会以错误告终。

 redirect_to(order_invoice_path(@order, @invoice)

No route matches {:action=>"show", :controller=>"invoices", :order_id=>nil, :id=>#<Invoice id: 6, order_id: 17, invoice_id: nil, created_at: "2011-06-26 17:49:01", updated_at: "2011-06-26 17:49:01">}

最佳答案

您似乎在 Controller 中执行了以下操作:

redirect_to(order_invoice_path(@order, @invoice), :notice => 'Invoice was successfully updated.')

但是你在某处设置了@order吗?

无论如何,你最好做:

redirect_to(order_invoice_path(@invoice.order, @invoice), :notice => 'Invoice was successfully updated.')

关于ruby-on-rails-3 - Rails 嵌套路由,更新 Redirect_To 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com