开发者

Rails3 Nested Routes, Update Redirect_To Problem

I'm trying to get my routing to work properly after I update an invoice.

I have an order with many invoices and the following in my routes.rb

resources :orders do
 resources :invoices
end

resources :invoices

And in my 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

I can create an invoice through:

/orders/1/invoices/new

But when I save or update, I'm sent to:

/invoices/1

I ne开发者_运维技巧ed to redirect back to the order invoice path:

/orders/1/invoices/1

Have also tried changing the redirect to:

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

This works but it sends me to the wrong url with the order id being the same as invoice id...

Any help appreciated.

-- UPDATE --

If I try the following in my invoices controller, I end up with an error too..

 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">}


It seems you do the following in your controller:

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

But do you set @order somewhere?

Whatever, you'd better do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜