0

這些是我的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

+0

請始終在日誌中填寫您的問題。你可以在這裏閱讀更多關於調試你的rails應用程序的地方:http://nofail.de/2013/10/debugging-rails-applications-in-development/ – phoet

回答

0

除了已經給出的答案,您應該嘗試f.simple_fields_for而不是simple_fields_for來表示嵌套結構。否則,@invoice.invoice_line_items.build建成項目不填充表單數據:

<%= f.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 %> 
與改變的創建操作(見阿米特·夏爾馬後)

一起:

def create 
    @invoice = current_user.invoices.create(invoice_params) 
    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 

和變化「invoice_params」 (詳見後面的8bithero):

def invoice_params 
params.require(:invoice).permit(:adress_sender, :adress_recipient, :status, :user_id, :customer_id, invoice_line_items_attributes: [:id, :description, :price, :amount]) 
end 

這應該可以做到。

0

嘗試下面我希望它會幫助你。

def new 
    @invoice = Invoice.new 
    @invoice.invoice_line_items.build 
    end 

    def create 
    @invoice = current_user.invoices.create(invoice_params) 

    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 
1

首先略微的東西無關你的問題 - 爲set_invoice叫你總是創建一個新的invoice_line_item,甚至毀滅的時候。如果您試圖包含給定發票的所有invoice_line_items,那麼您應該可以採取類似@invoice.includes(:invoice_line_items).where(id: params[:id])這樣的操作,以便加載這些項目。

至於你的問題...對於incoive_params,invoice_line_items應該是invoice_line_items_attributes(這也是很好的包括:id爲更新)。

試試看看它是否有效。同樣在創建操作中,我不認爲你需要添加@invoice.invoice_line_items.build,因爲你已經在上面的命令中包含了嵌套表單的參數。通過做@invoice.invoice_line_items.build它看起來像你簡單地爲@invoice創建另一個invoice_line_item,但沒有發言params。

(寫的有點倉促的回答希望這是有道理的。)

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 

# 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_attributes: [:id, :description, :price, :amount]) 
end 

```

側面說明:地址的拼寫錯誤。不知道這是否會導致鐵路多元化的麻煩。