1

I've got an problem with my update action for a nested resource.

In my app, my orders have many invoices.

Creating a new invoice, I correctly end up with the following url:

/orders/11/invoices/new

And when I edit the invoice, again, it's all correct:

/orders/11/invoices/3/edit

This works fine when the save is a success, however if the validation fails, it routes back to:

/invoices/3

I have the following in my invoices controller:

def update
   # @order = Order.find(params[:order_id])
   # @invoice = @order.invoices.find(params[:id])
    @invoice = Invoice.find(params[:id])

    respond_to do |format|
      if @invoice.update_attributes(params[:invoice])
        format.html { redirect_to(order_invoice_path(@invoice.order, @invoice), :notice => 'Invoice was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @invoice.errors, :status => :unprocessable_entity }
      end
    end
  end

def edit
    @invoice = Invoice.find(params[:id])
    3.times { @invoice.invoice_items.build }
  end

I'm assuming I need to edit the @invoice.errors part but I don't know what to change it to?

Any help appreciated. Jx

Jenny Blunt
  • 1,576
  • 1
  • 18
  • 41

2 Answers2

1

in your form you should add your order, like this:

<%= form_for [@order, @invoice] ... do |f| %>
  ...
<% end %>

And then uncomment this two lines

# @order = Order.find(params[:order_id])
# @invoice = @order.invoices.find(params[:id])

so your form will send its request to POST /orders/XX/invoices/XX

fl00r
  • 82,987
  • 33
  • 217
  • 237
  • Hi, thank. Tried as you suggested but I end up with both the success and failures redirecting to /invoice/3 and then a routing error? Jx – Jenny Blunt Aug 04 '11 at 23:04
  • Did you remove **resources :invoices** from your routes.rb file? – rookieRailer Aug 05 '11 at 03:16
  • @rookieRailer - we need :invoices for the accounts staff so have left that in. – Jenny Blunt Aug 05 '11 at 15:48
  • I guess thats why its behaving that way. Having two routes to a single action certainly creates ambiguity. You might want to think over the design of your routing. Ca you provide some more info, maybe attach some more code snippets. – rookieRailer Aug 05 '11 at 19:31
  • @rookierailer. I guess I don't need both routes as I can link straight to the nested on... – Jenny Blunt Aug 07 '11 at 08:10
1

When updating failed, you use "render" (comparing with the "redirect_to" in the succeeding path), this brings you to invoice editing path by default. You can use "redirect_to" here to keep the URI path you want, but need remembering to preserve the models' states so your users don't need to fill the entire form all over again.

A detail instruction can be found here: How to make a render :edit call show the /edit in the address bar

Yan

Community
  • 1
  • 1
gzbigegg
  • 36
  • 1
  • Hi, I've used redirect to which works fine however I lose my error messages and need the extra actions in the edit funtion. I'm going to try and remove resources :invoices from my routes and see if that helps – Jenny Blunt Aug 07 '11 at 08:12