Muhammad Yaseen Muhammad Yaseen - 1 year ago 56
Ruby Question

Unable to grab the same ID inside the form while edit action - Ruby on Rails

I have an Invoices app so that users can create the invoices.

The issue is when I create a New Invoice it's ID is 1 ( http://localhost:3000/invoices/1 ) as this is the first invoice that is Invoice # 1 but when I go to Edit link it still shows ( http://localhost:3000/invoices/1/edit ) but with Invoice # 2, as it must grab the same ID i.e.,1 retaining Invoice # 1.

Here's an screenshot as below;



<div class="form-group">
<div class="col-xs-4"><label>Invoice #</label></div>
<div class="col-xs-8 invoice_num"><span id="invoice_number"><%= (Invoice.last.present? ? ( + 1) : 1) %></span></div>

I don't know where I making the mistake.

Any suggestions are most welcome.

Thank you in advance.

Answer Source

If you'r using default rails controller methods for editing and creating invoices you can use @invoice instance variable to access it's id when you want to edit an invoice:

<%= %>