Search code examples
ruby-on-railsassociationsnested-attributes

Show/edit action not rendering attributes of nested form although nested attributes are being created in DB


I have set up a nested form and am able to save the form to my DB, but when I try to render the forms data in the show/edit action the form fields are blank.

Here are my models

invoice.rb
class Invoice < ActiveRecord::Base
has_many :items
accepts_nested_attributes_for :items
end

item.rb
class Item < ActiveRecord::Base
belongs_to :invoice
accepts_nested_attributes_for :invoice
end

And my controller

def index
@invoices = Invoice.all
end

def show
@invoice = Invoice.find(params[:id])
@item = @invoice.items.build
end

# GET /invoices/new
def new
@invoice = Invoice.new
@invoice.items.build #
end

# GET /invoices/1/edit
def edit
@invoice.items.build #
end

def create
@invoice = Invoice.new(invoice_params)

respond_to do |format|
  if @invoice.save
    format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
    format.json { render :show, status: :created, location: @invoice }
  else
    format.html { render :new }
    format.json { render json: @invoice.errors, status: :unprocessable_entity }
   end
 end
end


 private
 def set_invoice
  @invoice = Invoice.find(params[:id])
 end

 def invoice_params
  params.require(:invoice).permit(:amount, :company, :contragent, :currency, :date, items_attributes: [ :item_name, :item_description, :item_cost, :item_quantity, :item_price ])
end
end

Here is my form

  <%= simple_form_for(@invoice) do |f| %>

   <%= f.input_field :amount, class: "form-control" %>
   <%= f.input_field :company, class: "form-control" %>
   <%= f.input_field :currency, class: "form-control" %>
   <%= f.input_field :contragent, class: "form-control" %>
   <%= f.label :date, required: false %>

    <%= f.fields_for :items do |h| %>
    <%= h.error_notification %>
    <%= h.label :item_name %>
    <%= h.input_field :item_name, class: "form-control" %>
    <%= h.label :item_description %>
    <%= h.input_field :item_description, class: "form-control" %>
    <%= h.label :item_cost %>
    <%= h.input_field :item_cost, class: "form-control" %>
    <%= h.label :item_quantity %>
    <%= h.input_field :item_quantity, class: "form-control" %>
    <%= h.label :item_price %>
    <%= h.input_field :item_price, class: "form-control" %>
    </br>
   <% end %>

   <%= f.button :submit, 'Submit Payment', class: 'btn btn-warning btn-sm', id: "submit_invoice" %>    
<% end %>

After saving the form I get this in my rails console

2.1.1 :018 > p = Invoice.where(id: 38).limit(1)
Invoice Load (0.1ms)  SELECT  "invoices".* FROM "invoices" WHERE "invoices"."id" = ? LIMIT 1  [["id", 38]]
=> #<ActiveRecord::Relation [#<Invoice id: 38, amount: #<BigDecimal:7f8b6994b000,'0.875E3',9(18)>, company: "Alex", contragent: "Hi", currency: "NZD", date: "2016-02-17", created_at: "2016-01-18 22:23:36", updated_at: "2016-01-18 22:23:36">]>


2.1.1 :026 > p = Item.where(id: 7).limit(1)
Item Load (0.1ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = ? LIMIT 1  [["id", 7]]
 => #<ActiveRecord::Relation [#<Item id: 7, item_name: "Test", item_description: "This is a test", item_cost: "200", item_quantity: "1", item_price: "400", created_at: "2016-01-18 22:23:36", updated_at: "2016-01-18 22:23:36", invoice_id: 38>]> 
2.1.1 :027 >

So the Invoice ID is set on the item record, how do i connect it to the invoice in the show and edit action?


Solution

  • You need nested resources:

    resources :invoices do
      resources :items
    end
    

    This will allow your router to access items in the invoices:

    /invoices/1/items/38

    Read more about nested resources: Rails Routing from the Outside In: Nested Resources.