這些是我的2個模型,沒有什麼複雜的。Rails創建嵌套屬性,但字段保持空
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
這是修改嵌套屬性的simple_form。
<%= 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 %>
這是控制器根據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
發票行項目正在創建。但只是創建和更新日期和ID。所有領域如描述,價格和金額都是空的。另外allow_destroy似乎不起作用。不幸的是,在服務器控制檯中沒有錯誤:/
編輯,建議創建的控制檯日誌。
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)
任何想法或建議?
問候 denym
請始終在日誌中填寫您的問題。你可以在這裏閱讀更多關於調試你的rails應用程序的地方:http://nofail.de/2013/10/debugging-rails-applications-in-development/ – phoet