These are my 2 models, nothing complicated.
class Invoice
belongs_to :user
has_many :invoice_line_items
accepts_nested_attributes_for :invoice_line_items, allow_destroy: true
end
class InvoiceLineItem
belongs_to :invoice
end
This is the modified simple_form for the nested attributes.
<%= simple_form_for(@invoice) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :adress_sender %>
<%= f.input :adress_recipient %>
<%= f.input :status %>
<%= simple_fields_for :invoice_line_items do |invoice_line_items_form| %>
<%= invoice_line_items_form.input :description %>
<%= invoice_line_items_form.input :price %>
<%= invoice_line_items_form.input :amount %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
This is the controller according to the official documentation of rails.
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
@invoices = current_user.invoices
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
@invoice = Invoice.new
@invoice.invoice_line_items.build
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
@invoice = current_user.invoices.new(invoice_params)
@invoice.invoice_line_items.build
respond_to do |format|
if @invoice.save
format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
format.json { render action: 'show', status: :created, location: @invoice }
else
format.html { render action: 'new' }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if @invoice.update(invoice_params)
format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
@invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions and check if the user has access to it.
def set_invoice
@invoice = current_user.invoices.find(params[:id])
@invoice.invoice_line_items.build
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:adress_sender, :adress_recipient, :status, :user_id, :customer_id, invoice_line_items: [:description, :price, :amount])
end
end
The invoice line items are getting created. But just the create and update date and the id. All fields like description, price and amount are empty. Also the allow_destroy seems not to work. Unfortunally there is no error in the server console :/
Edit, as suggested the console log of the creation.
Started POST "/invoices" for 127.0.0.1 at 2013-10-21 15:36:42 +0200
Processing by InvoicesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mFdXPbYF+Lsb1mugWkdykBkJ1iSrzZoREL5Alw6phhQ=", "invoice"=>{"adress_sender"=>"awfawf", "adress_recipient"=>"awfgaw", "status"=>"awgag"}, "invoice_line_items"=>{"description"=>"awga", "price"=>"awgwa", "amount"=>"awg"}, "commit"=>"Create Invoice"}
User Load (2.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
(0.3ms) BEGIN
SQL (7.0ms) INSERT INTO "invoices" ("adress_recipient", "adress_sender", "created_at", "status", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["adress_recipient", "awfgaw"], ["adress_sender", "awfawf"], ["created_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00], ["status", "awgag"], ["updated_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00], ["user_id", 1]]
SQL (0.9ms) INSERT INTO "invoice_line_items" ("created_at", "invoice_id", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00], ["invoice_id", 15], ["updated_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00]]
(4.2ms) COMMIT
Redirected to http://localhost:3000/invoices/15
Completed 302 Found in 60ms (ActiveRecord: 14.5ms)
Any idea or suggestion?
best regards denym